-4

I'm working on Rails. In my code base, I see a line that using Arel::SqlLiteral like this:

result = Arel::Nodes::SqlLiteral.new(<<-SQL
  CASE WHEN condition1 THEN calculation1
  WHEN condition2 THEN calculation2
  WHEN condition3 THEN calculation3
  ELSE default_calculation END
SQL)

I understand what this code piece do. The thing I don't understand is its grammar, at this point:

Arel::Nodes::SqlLiteral.new(<<-SQL
  ...
  SQL
)

So in ruby, what is the grammar of <<- follow by name, and then at last block we call that name.

thanks

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Trần Kim Dự
  • 5,872
  • 12
  • 55
  • 107
  • I really don't understand why after marking duplicate, I still receive down vote :D completely nonsense :D I think those people down vote me don't have happy life ^^ – Trần Kim Dự Dec 20 '16 at 12:50
  • Moreover, whether they can google `<<-` operator means :D I have tried without result. Hopefully they should think twice before some actions :D respect people around them ^^ a little down votes don't make anything change but them. They will become more and more hatred. – Trần Kim Dự Dec 20 '16 at 12:53

1 Answers1

1

The keyword you're looking for is "Heredoc".

https://ruby-doc.org/core-2.2.0/doc/syntax/literals_rdoc.html#label-Here+Documents

It's mainly used to prettify large texts and common practice for shells/shellscripts. The marker on top indicates the beginning of a heredoc and the marker on bottom (which must not be indented unless you place a “-” before the opening marker) specifies the end.

m0gg
  • 134
  • 1
  • 6
  • thanks. really don't know why someone down vote your answer :D it helps me. And at least not doing something wrong ^^ poor them. – Trần Kim Dự Dec 20 '16 at 13:15