What does "<<" (double less) mean without an argument?

I found this inside a method that I want to override in formtastic Gem. The method looks like this:

 def to_html input_wrapping do hidden_field_html << label_with_nested_checkbox end end 

What does << in the third line do? I know what it does with arrays, but here I have no idea.

+5
source share
2 answers

You can read it as follows:

 hidden_field_html << label_with_nested_checkbox 

label_with_nested_checkbox - the argument concatenated at the end of hidden_field_html - they split it into two lines for "clarity"

+8
source
  • Class inheritance uses < , not << , and the first has nothing to do with the << method.

  • Ruby has a high tolerance for indentation in space; almost anywhere, any number of spaces could be placed, including newlines, between a function call and an argument.

E. g :.

 'aaa'. length #⇒ 3 

and

 'aaa' .length #⇒ 3 

both are absolutely correct.

  1. << is a generic method that can be overwritten in any class. Here's supposedly String#<< method that adds an argument to a string receiver.

In general, you can rewrite this method in any arbitrary class:

 class A attr_accessor :var def initialize @var = 5 end def << value @var += value end end a = A.new a.var #⇒ 5 a << 37 a.var #⇒ 42 
+1
source

All Articles