tags:

views:

206

answers:

6

Hi, I recently used the <<- operator to output a multi-line string, this way...

<<-form
  <h1>Name to say hi!</h1>
  <form method="post">
    <input type="text" name="name">
    <input type="submit" value="send">
  </form>
form

But I stole the <<- operator from some Open Source code, but I didn't find any documentation on it.

I kinda figured out that it works the same as in bash:

$ cat <<EOF >> form.html
> <h1>Name to say hi!</h1>
> <form method="post">
>   <input type="text" name="name">
>   <input type="submit" value="send">
> </form>
> EOF

Does it works that way? I just wanna find documentation on it...

PS: My full example is a simple get/post Sinatra app

A: 

See Programming Ruby. Search for "here document".

Note that << can also mean Append for strings and it is overloaded for a number of classes (e.g. Array). See Ruby core documentation.

Sinan Ünür
+3  A: 

This is the Ruby "here document" or heredoc syntax. The addition of the - indicates the indent.

JacobM
Thank you I didn't even know what was the name of that :)
igorgue
+1  A: 

This post will tell you everything you need to know about the "heredoc" string syntax. In addition, you can view the rubydoc page for string syntax.

Splash
+8  A: 
JRL
A: 

The reason why you cannot find any documentation on the <<- operator is because it isn't an operator. It's literal syntax, like ' or ".

Specifically, it's the here document syntax which is one of the many syntactic forms of string literals in Ruby. Ruby here documents are similar to POSIX sh here documents, but handling of whitespace removal is different: in POSIX sh here documents delimited by <<-, only leading tabs are removed, but they are removed from the contents of the string, whereas in Ruby all leading whitespace is removed, but only from the delimiter.

Jörg W Mittag
yep, I didn't know that, thanks :)
igorgue