Given a string, how to append a carriage return followed by another string?

ruby, ruby-on-rails

Solution

It really depends on what you are outputting to.

$STDOUT:

puts "Hello\n\n#{myURL}"

or

puts "Hello"
puts
puts myURL

or

puts <<EOF
Hello

#{myURL}
EOF

If you are outputting this in an `html.erb` or `.rhtml` document:

<%= "Hello<br /><br />#{myURL}" %> # or link_to helper

If you already have a string like `string1` then you can append to it using either `+=` or `<<`:

string1  = "Hello world, join my game:"
myUrl    = "http://example.com"
string1 += "\n\n#{myUrl}"

or:

string1 = "Hello world, join my game:"
myUrl   = "http://example.com"
string +=<<EOF

#{myUrl}
Here's some other details
EOF

Problem

I have a string like so `string1`: ``` Hello World, join my game: ``` I would like to make string1 become: ``` Hello World, join my game: http://game.com/url ``` How can I append a carriage return with ruby and then a link from another variable? THanks

Original source