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.
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.
EOS means end of string. it is displayed at the end of the string.
(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 !=
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With