In PHP, the following would allow me to create a string without having to escape quotes..
$string = <<<EOD
',. whatever <"",'
EOD;
echo $string;
Is there anything similar to it in Ruby/Rails?
In PHP, the following would allow me to create a string without having to escape quotes..
$string = <<<EOD
',. whatever <"",'
EOD;
echo $string;
Is there anything similar to it in Ruby/Rails?
This is called a here doc. From the link, the ruby way would be:
puts <<-GROCERY_LIST
Grocery list
------------
1. Salad mix.
2. Strawberries.*
3. Cereal.
4. Milk.*
* Organic
GROCERY_LIST
The result:
$ ruby grocery-list.rb
Grocery list
------------
1. Salad mix.
2. Strawberries.*
3. Cereal.
4. Milk.*
* Organic
Ruby supports multiline strings by providing two types of here doc syntax. The first syntax uses and additional dash, but allows you to indent the "end of here doc" delimiter ('eos' in the example).
<<-eos
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
eos
Another here doc syntax doesn't require you to use the dash, but it does require that the "end of here doc" delimiter is in column 1 (or there are no spaces that precede it).
<<eos
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
eos
Ruby heredocs are pretty much the same, with minor changes, and they come in 2 flavours:
1) End-of-heredoc must be at the start a line:
string = <<EOD
',. whatever <"",'
EOD
puts string
2) End-of-heredoc may be preceeded by whitespace:
string = <<-EOD
',. whatever <"",'
EOD
puts string
you can do it like this
string = <<EOD
',. whatever <"",'
EOD
puts string