Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "<<-" mean in Ruby?

Tags:

ruby

For example:

code = <<-EOH     bundle install     bundle exec unicorn -c /etc/unicorn.cfg -D EOH 

What does this code do? What is <<- called?

like image 266
camdub Avatar asked May 12 '12 04:05

camdub


People also ask

What does << mean in Ruby?

In ruby '<<' operator is basically used for: Appending a value in the array (at last position) [2, 4, 6] << 8 It will give [2, 4, 6, 8] It also used for some active record operations in ruby.

What is << in Ruby on Rails?

It lets you add items to a collection or even concatenate strings.

What is <% in Ruby?

Code in <% %> (without equal) is executed "with no substitution back into the output" means you want to execute code WITHOUT any output, like a loop and the best part is, it can be used with a non ruby code.


2 Answers

It's called heredoc. An easy way to define multiline strings which may include single or double quotes without needing to escape them.

See more here, for example.

Often you use heredocs to define large chunks of code. Some editors know about this and can highlight syntax for you there (if you specify language). Look:

strings vs heredocs

like image 138
Sergio Tulentsev Avatar answered Sep 18 '22 12:09

Sergio Tulentsev


Looks to me like heredoc. The - allows the ending delimiter to ignore whitespace before it.

A simple Google Search gave me this.

like image 22
Niet the Dark Absol Avatar answered Sep 22 '22 12:09

Niet the Dark Absol