Difference between %{} and #{}

HI Experts,

i need to know the difference between %{} and #{}

waiting for the response

regards,
Bala

first of all, i don’t really think u’d be using %{} too much of the time
(you might use some of the options available, like %q{} which is like
single quotes, and %Q{} which is like double-quotes.
basically:

%{} -> turns the stuff written into a string.
#{} -> if inside double quotes, eval the code inside.

hope the following example helps out a bit more:

[email protected]:~/ruby$ cat str.rb

#!/usr/bin/ruby

one=1
two=2

puts “the variable ‘one’ used with #{} is #{one}”
puts “this is one with %{} in a string: %{one}”
puts “this is one with %{} ruby (not within a string):” + %{one}
puts “”; puts “notice something else though:”; puts “”
puts “this is two with ruby and %q{}:” + %q{#{two}}
puts “this is two with ruby and %Q{}:” + %Q{#{two}}

[email protected]:~/ruby$ ruby str.rb

the variable ‘one’ used with #{} is 1
this is one with %{} in a string: %{one}
this is one with %{} ruby (not within a string):one

notice something else though:

this is two with ruby and %q{}:#{two}
this is two with ruby and %Q{}:2

Thanks a log Shai. :slight_smile:

On Mar 17, 3:59 pm, Shai R. [email protected]

oops sorry that was lot. :slight_smile:

On Mar 17, 3:59 pm, Shai R. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs