Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of <<<EOD 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?

like image 647
Marco Avatar asked Jul 20 '10 19:07

Marco


People also ask

What is %{} in ruby?

This is percent sign notation. The percent sign indicates that the next character is a literal delimiter, and you can use any (non alphanumeric) one you want. For example: %{stuff} %[stuff] %?

What is EOS in Ruby?

EOS means end of string. it is displayed at the end of the string.

How do you make a new line in Ruby?

"\n" is newline, '\n\ is literally backslash and n.


4 Answers

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
like image 196
Mike Tunnicliffe Avatar answered Oct 14 '22 17:10

Mike Tunnicliffe


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
like image 32
Alex Avatar answered Oct 14 '22 17:10

Alex


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
like image 45
ccheneson Avatar answered Oct 14 '22 17:10

ccheneson


It's called a heredoc, and it's <<WHATEVER in Ruby.

like image 22
Chuck Avatar answered Oct 14 '22 18:10

Chuck