Ruby for the Newbie - A Few Notes About Formatting
(Page 2 of 4 )
Let's say you don't want to write puts on each line when writing your text to the screen. There are a few options for this:
print "I like hamburgers. nThey are good" # sorry, I'm hungry
The above code would display:
I like hamburgers.
They are good.
The n tells the program to skip to the next line. You can also use the <<Whatever document.
<Note: You don't have to use the word Whatever; any word is acceptable>
print <<WHATEVER
I like hamburgers.
They are good.
WHATEVER
This would result in the same text as our previous example:
I like hamburgers.
They are good.
In addition to typing in the quotes (" ") yourself, you can let Ruby add them for you. For single quotes use %q, for double quotes use %Q.
puts %Q/I like apples/
puts %q/Me too/
The above code would display the following text:
"I like apples"
'Me too'
You can also concatenate (or join together) strings very simply in Ruby, using the + symbol.
puts "Come over here" + "now"
Would print out: "Come over here now."
Next: Variables >>
More Ruby-on-Rails Articles
More By James Payne