This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Substituting Variables Into Strings
CodeExpectedActual
number = 5
"The number is #{number}."
"The number is 5." "The number is 5."
"The number is #{5}."
"The number is 5." "The number is 5."
"The number after #{number} is #{number.next}." 
"The number after 5 is 6." "The number after 5 is 6."
"The number prior to #{number} is #{number-1}."   
"The number prior to 5 is 4." "The number prior to 5 is 4."
"We're ##{number}!"
"We're #5!" "We're #5!"
"#{number}" == '5'
true true
%{Here is #{class InstantClass
   def bar 
      "some text" 
    end 
  end 
  InstantClass.new.bar
}.}
"Here is some text." "Here is some text."
"I've set x to #{x = 5; x += 1}."
"I've set x to 6." "I've set x to 6."
x
6 6
"\#{foo}"
"\#{foo}" "\#{foo}"
'#{foo}'
"\#{foo}" "\#{foo}"
name = "Mr. Lorum"
email = <<END
Dear #{name},
Unfortunately we cannot process your insurance claim at this
time. This is because we are a bakery, not an insurance company.
Signed,
 Nil, Null, and None
 Bakers to Her Majesty the Singleton
END
<<page_break
There once was a man from Peru
This limerick ends on page 2.
page_break
"There once was a man from Peru\nThis limerick ends on page 2.\n" "There once was a man from Peru\nThis limerick ends on page 2.\n"