Forum: Ruby Multi-line string

5e7f3a5f140ffe0175a38875dce9a564?d=identicon&s=25 Nate Quash (bklynate)
on 2014-05-09 21:00
Hi,

I recently came to ruby after spending a few months learning Python.

I was wondering if there is a way similar to Python's triple quote ' """
'
that allows for a comment to be printed on multi-lines

say for instance:

puts "The choice is yours kid: A) Shake down the owner
                               B) Rob the storekeeper"

You wanted this to print exactly in the format as you see here...

how would you do this?
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2014-05-09 22:09
The code you wrote is actually valid ruby, although you'll need to
offset a bit because of "puts".

irb(main):001:0> puts "The choice is yours kid: A) Shake down the owner
irb(main):002:0"                                B) Rob the storekeeper"
The choice is yours kid: A) Shake down the owner
                               B) Rob the storekeeper

If you're writing complex multiline strings, you might want to have a
look at Heredocs:
http://rubyquicktips.com/post/4438542511/heredoc-and-indent
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.