Refinement on Ruby << Operator

I am new to Ruby and I wonder what the << operator is. When I googled this operator, it says that it is a binary left-shift operator given in this example:

a << 2 will give 15 , which is 1111 0000

however, it does not look like a “binary left-shift operator” in this code:

 class TextCompressor attr_reader :unique, :index def initialize(text) @unique = [] @index = [] add_text(text) end def add_text(text) words = text.split words.each { |word| do add_word(word) } end def add_word(word) i = unique_index_of(word) || add_unique_word(word) @index << i end def unique_index_of(word) @unique.index(word) end def add_unique_word @unique << word unique.size - 1 end end 

and this question does not seem to apply in the code I gave. So, with the code that I have, how does the Ruby << operator work?

+4
source share
5 answers

Ruby is an object oriented language. The fundamental principle of object orientation is that objects send messages to other objects, and the recipient of the message can reply to the message in any form convenient for him. So,

 a << b 

means that a decides what this should mean. It is impossible to say what << means without knowing what a .

As a general convention, << in Ruby means "append", that is, it adds its argument to its receiver and then returns the receiver. Thus, for Array it adds an argument to the array, for String it concatenates strings, for Set it adds an argument to a set, for IO it writes to a file descriptor, etc.

As a special case, for Fixnum and Bignum , it performs a bitwise left shift of the two-component Integer representation. This is mainly because C and Ruby are affected by C.

+19
source

<<this is just a method. This usually means “add” in a way, but it can mean anything. For strings and arrays, this means add / add. For integers, a bit shift.

Try the following:

 class Foo def << (message) print "hello " + message end end f = Foo.new f << "john" # => hello john 
+6
source

In Ruby, operators are just methods. Depending on the class of your variable, << can do different things:

 # For integers it means bitwise left shift: 5 << 1 # gives 10 17 << 3 # gives 136 # arrays and strings, it means append: "hello, " << "world" # gives "hello, world" [1, 2, 3] << 4 # gives [1, 2, 3, 4] 

It all depends on what class defines << .

+2
source
<<function, according to http://ruby-doc.org/core-1.9.3/Array.html#method-i-3C-3C , is an add function. It adds the passed value to the array and then returns the array itself. Ruby objects often have functions defined on them that in other languages ​​will look like an operator.
+1
source

<< is an operator that is syntactic sugar for calling the << method for a given object. On Fixnum it is defined as a left-shift bit , but it has different meanings depending on the class on which it is defined. For example, for Array it adds (or rather, "shovels") an object to an array .

We can see here that << is really just syntactic sugar for calling a method:

 [] << 1 # => [1] [].<<(1) # => [1] 

and therefore, in your case, it just calls << on @unique , which in this case is Array .

+1
source

Source: https://habr.com/ru/post/1401715/


All Articles