jazzlark
jazzlark

Reputation: 35

gets.chomp method excercise in learn to program book

I am doing excercise 5.6 out of "Learn to Program" for a class. I have the following:

    puts 'What\'s your first name?'
    first = gets.chomp
    puts 'What\'s your middle name?'
    middle = gets.chomp
    puts 'What\'s your last name?'
    last = gets.chomp
    puts 'Hello, Nice to meet you first middle last'

And I have tried the following:

     puts 'What is your first name?'
     first = gets.chomp
     puts 'What is your middle name?'
     middle = gets.chomp
     puts 'What is your last name?'
     last = gets.chomp
     puts 'Hello, Nice to meet you #{first} #{middle} #{last}'

When I get the last "puts" it won't get the first, middle and last name that I wrote in. It says, "Hello, nice to meet you first, middle, last....Instead of Joe John Smith for example. What am I doing wrong?

Thank you so much for your help! I really appreciate it!

Upvotes: 2

Views: 128

Answers (3)

Paul Rubel
Paul Rubel

Reputation: 27222

Strings within single quotes ' do not replace variables with their values. Try using double quotes " like so:

puts "Hello, Nice to meet you #{first} #{middle} #{last}"

Single qoutes are nice if you want the string as you typed it, double quotes are useful when you want to replace variable names with their values.

Upvotes: 3

Arup Rakshit
Arup Rakshit

Reputation: 118261

You can also use %Q to have the same effect as you get with double quotes ":

x = 12
puts %Q(I need #{x} pens)
# >> I need 12 pens

Upvotes: 0

Billy Chan
Billy Chan

Reputation: 24815

When using interpolation, use double quotes " instead of single quotes '

Upvotes: 3

Related Questions