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.
source share