1

I know that this may be a very basic question but i'm struggling to find a clear answer. What does using this, #{} , mean in Ruby? What does it do?

Thanks

Robbo
  • 1,292
  • 2
  • 18
  • 41
  • 4
    I don't know why you asked this question. Basic Google will give you the information. Read [here](http://www.ruby-doc.org/core-2.1.1/doc/syntax/literals_rdoc.html#label-Strings). Or here in Stacoverflow itself. See this [search](http://stackoverflow.com/search?q=%5Bruby%5D+%22%23%7B%7D%22) – Arup Rakshit May 14 '14 at 15:54
  • 2
    @ArupRakshit you can't google for `#{}`, can you? – Stefan May 14 '14 at 15:56
  • 1
    Yea I couldn't google #{}. Thanks for everyone's help so quickly though :) – Robbo May 14 '14 at 16:00
  • 1
    @James But before increasing the load in SO of such duplicate questions, you can search here itself..right ? – Arup Rakshit May 14 '14 at 16:01
  • @Stefan Yes.. not lots of hits.. atleast 2 I got. see [this](https://www.google.co.in/search?q=Ruby+%23%7B%7D&oq=Ruby+%23%7B%7D&aqs=chrome..69i57j0l5.4078j0j8&sourceid=chrome&es_sm=121&ie=UTF-8#q=Ruby+syntax+%23%7B%7D). – Arup Rakshit May 14 '14 at 16:05
  • 1
    If I don't know that it's called string interpolation and I got not results with my use of #{} in the question how can I search for it. Appreciate a little less condescension. – Robbo May 14 '14 at 16:26
  • @James I found the question I linked serching here on SO for `[ruby] "#{}"`. – toro2k May 14 '14 at 16:30
  • I tried and didn't find, sorry. But I really appreciate everyones help! – Robbo May 14 '14 at 16:39

5 Answers5

7

It is used for String interpolation: ( wikipedia, ctrl+f "ruby" )

apples = 4
puts "I have #{apples} apples"
# or
puts "I have %s apples" % apples
# or
puts "I have %{a} apples" % {a: apples}

The output will be:

I have 4 apples

String interpolation, Definition:

In Ruby, string interpolation refers to the ability of double-quoted strings to execute Ruby code and replace portions of that strings (denoted by #{ ... }) with the evaluation of that Ruby code.

It is the most common way to inject data (usually the value of a variable, but it can be the evaluation of any Ruby code) into the middle of a string.


A thing to know:

puts "This User name is #{User.create(username: 'Bobby')}!"

This will make an implicit call of .to_s on the User's instance object.

If you defined the method .to_s on the User model:

class User
  def to_s
    self.username
  end

It would output:

puts "This User name is #{User.create(username: 'Bobby')}"
# => "This User name is Bobby"
Community
  • 1
  • 1
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
1

It is for String Interpolation..

In Ruby, there are three ways of interpolation, and #{} is just one way.

apples = 4
puts "I have #{apples} apples"
# or
puts "I have %s apples" % apples
# or
puts "I have %{a} apples" % {a: apples}
ddavison
  • 28,221
  • 15
  • 85
  • 110
1

It's called String Interpolation

In Ruby, string interpolation refers to the ability of double-quoted strings to execute Ruby code and replace portions of that strings (denoted by #{ ... }) with the evaluation of that Ruby code. It is the most common way to inject data (usually the value of a variable, but it can be the evaluation of any Ruby code) into the middle of a string.

print "What is your name? "
name = gets.chomp
puts "Hello, #{name}"

Note that any code can go inside the braces, not just variable names. Ruby will evaluate that code and whatever is returned it will attempt to insert it into the string. So you could just as easily say "Hello, #{gets.chomp}" and forget about the name variable. However, it's good practice not to put long expressions inside the braces.

Author: Michael Morin

Mini John
  • 7,855
  • 9
  • 59
  • 108
  • 1
    [If you're going to quote somebody, you have to attribute the quote to them.](http://ruby.about.com/od/sz/g/String-Interpolation.htm). – Zach Kemp May 14 '14 at 16:03
  • downvoted because you used somebody's definition without giving him the credits for it... – MrYoshiji May 14 '14 at 16:04
  • The time i wrote this there was 1.) No answers yet, 2.) I included several links to the Articles. Dunno why you would downvote that, but it's Ok i guess, your Choice of empowering SO. – Mini John May 14 '14 at 16:26
  • Your answer still implies that you wrote the definition of String interpolation => That is why I downvoted, the fact that you included X links in your answer does not change anything because none of these links shows that you took the definition of Michael Morin! – MrYoshiji May 14 '14 at 16:32
  • Here we go! Thanks for respecting others' work – MrYoshiji May 14 '14 at 16:35
  • 1
    Dude, I'm TOTALLY respecting other's work. I don't know how you take this as your Argument. I linked to TWO of his articles where someone can CLEARLY see that he is the Author. And you are "Thanking" me for putting the author SPECIALLY in my Answer, when your's don't even have it... Don't know whats wrong here.. Jeesus – Mini John May 14 '14 at 16:38
  • 2
    @TheMiniJohn Be cool!! Enjoy your time with more answers and questions in SO... :-) – Arup Rakshit May 14 '14 at 18:20
0

That allows you to evaluate arbitrary ruby code within a string (double quotes only). So for example, in something like PHP you'd do

$my_var = "The value of a = " . $a;

But in ruby, you'd just do

my_var = "The value of a = #{a}"

This only works if the string is in double quotes, and NOT single quotes. If you us single quotes then you'll get the #{...} appear in the text

PaReeOhNos
  • 4,338
  • 3
  • 30
  • 41
0

You use it to represent a variable you want to print or puts out. For example:

puts "#{var}" would puts out the value stored within your variable var.