-1

I cannot print an input variable as part of a string.

name = gets
>>> Lizzie
print #{name}
>>> Lizzie

That works. However, when I want to do:

name = gets    
print 'Your name is: #{name}' 

it outputs:

Your name is: #{name}

Any help would be appreciated.

sawa
  • 165,429
  • 45
  • 277
  • 381
Lizzie
  • 53
  • 1
  • 6
  • 4
    Use `"` instead of `'` to quote your string. – Peter Huene Feb 15 '15 at 20:33
  • 2
    Actually, [Ruby: double vs single quotes](http://stackoverflow.com/questions/6395288/ruby-double-vs-single-quotes) has clearer answers. – Michael Berkowski Feb 15 '15 at 20:35
  • @MichaelBerkowski It's not a duplicate because the author most likely didn't know there was a difference between single and double quotes in Ruby. But in the end, yes it's the direction he should be pointed to. – Cyril Duchon-Doris Feb 15 '15 at 20:37
  • Oh, okay, never heard of single and double quotes (guess I am such a noob :) ). Thank you for the point out! – Lizzie Feb 15 '15 at 20:38
  • 1
    @CyrilDD "Duplicate" doesn't mean "exactly the same question", it means "there's already a suitable answer to your question over here, have a look!" And it causes future visitors to be directed to the most complete questions and answers. – Michael Berkowski Feb 15 '15 at 20:38
  • So I checked out the answer suggested, and the problem was I should have used double quote instead of single. Guess Ruby is a bit more sensitive than Python! – Lizzie Feb 15 '15 at 20:43
  • The concept of single quote vs double quote interpolation comes from perl, at least. that might make a good SO question - which languages support it? – DGM Feb 15 '15 at 20:52
  • @MichaelBerkowski Oh ok then you're absolutely right – Cyril Duchon-Doris Feb 15 '15 at 20:53

1 Answers1

1

Not sure why @peter_huene didn't post this as an answer, but just to complete this question the answer is:

name = gets    
print "Your name is: #{name}"

You use double quotes to get variable injection and other features. Single quotes are literal quotes with no post processing.

@Lizzie - Ruby isn't "more sensitive" - it offers you two explicit ways of handling strings, which is more flexible. Unfortunately this means a bit of "magic" in that strings behave differently depending on how quoted, but it's a feature not a peculiarity.

Steve Midgley
  • 2,226
  • 2
  • 18
  • 20