Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does double less than minus "<<-" sign mean anything in ruby?

Tags:

syntax

ruby

I am trying to get myself familiar with ruby syntax and coding styles(i am a newbie). I came across a code that uses <<-, what does this mean in Ruby? Code is

  def expectation_message(expectation)     <<-FE       #{expectation.message}       #{expectation.stack}     FE   end 

This is just a section of the whole code. Any help would be appreciated.

like image 758
B A Avatar asked Oct 26 '15 11:10

B A


People also ask

What does << In Ruby mean?

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 EOS in Ruby?

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

How do you write not equal to in Ruby?

(a == b) is not true. Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (a !=


1 Answers

There are various ways to define multi-line string in Ruby. This is one of them.

> name = 'John' > city = 'Ny' > multiline_string = <<-EOS > This is the first line > My name is #{name}. > My city is #{city} city. > EOS  => "This is the first line\nMy name is John.\nMy city is Ny city.\n"  > 

the EOS in above example is just a convention, you can use any string you like and its case insensitive. Normally the EOS means End Of String

Moreover, even the - (dash) is not needed. However, allows you to indent the "end of here doc" delimiter. See the following example to understand the sentences.

2.2.1 :014 > <<EOF 2.2.1 :015"> My first line without dash 2.2.1 :016">         EOF 2.2.1 :017"> EOF  => "My first line without dash\n        EOF\n"    2.2.1 :018 > <<-EOF 2.2.1 :019"> My first line with dash. This even supports spaces before the ending delimiter. 2.2.1 :020">    EOF  => "My first line with dash. This even supports spaces before the ending delimiter.\n"  2.2.1 :021 >  

for more info see https://cbabhusal.wordpress.com/2015/10/06/ruby-multiline-string-definition/

like image 97
illusionist Avatar answered Oct 21 '22 00:10

illusionist