vue87q
Last Updated: February 25, 2016
·
2.225K
· aalvarado
Me

Ruby operators, names, aliases and uses

When I started with Ruby there were a few names given to operators that I had a hard time trying to find out how they were named or what their purpose was.

I will try to list some of the different operators and their common given names and their uses, I think this will be a good reference for beginners and people who just forgot what's the name of a certain character combination they might encounter while reading code.

The list

Hashrocket

=> #hasrocket, arrow or just arrow. 

Used in conjunction with { } when defining hashes ( associative arrays or dictionaries in other languages ) .

Usage:

my_hash = { :key1 => 'my value', :key2 => 'other value' }
puts my_hash[ :key1 ]
#-> my value 

Symbol to proc

:& #symbol to proc

This is useful when you want to call a method on a collection of objects, it will execute the method by name after the &: on each element on the array/hash.

Usage:

my_array = [ 'foo', 'bar']
new_array = my_array.collect(&:reverse)
new_array.each{ |element| puts element }
#-> oof
#-> rab

Spaceship operator

<=> #Spaceship operator

Used independently, it will return 1, 0, or -1 depending if the left value is greater than, equal or less than the value of the right.

In addition; the spaceship operator is used along with the Enumerable mixing, it aids in defining a method for comparison, which in turn enables the mixer class to have several useful methods from the mixing like sorting, max, min, etc. For more information see the Enumerable mixin on the ruby docs: http://ruby-doc.org/core-1.9.3/Enumerable.html

Usage:

1 <=> 0
#-> 1
1 <=> 1
#-> 0
1 <=> 2
#-> -1

T-square operator or short circuit 'OR'

||= #t-square or short-circuit or

We use the operator to initialize a value only if the left side of the assignment yields a falsy ( nil ) or false value ; any other value will evaluate to true and will not assign the value on the right.

Usage:

foo ||= 'bar'
puts foo
#-> bar

foo ||= 'foobar' #since foo was initialized with 'bar' on the first assignment,
#foo will be truthy ( not false or nil) and will skip
#the assignment
puts foo
#-> 'bar'

Shovel or left shift operator

This operator has multiple uses, when used when the left value is an array it will call the push method and pass the right side as the argument. This can be chained multiple times.

When the left side is a string, it will call the concat method on the left side and pass the right side as an argument.

The other use is bit shifting, which are operations on numbers at bit level. For example take the following binary number:
0001 represents a 1, if you shift this one space to the right, it becomes a 2: 0010.
Shift it once again and you have 4: 0100
one more time and you now have 8: 1000

Usage:

# On arrays:
foo = []
foo << 'foo'
foo << 'bar'
foo << 'fizz' << 'buzz'
foo.each { |element| puts element }
#-> foo
#-> bar
#-> fizz
#-> buzz

# On strings:
my_string = 'foo'
my_string << 'bar'
puts my_string
# -> foobar

# On numbers
 1 << 0
 #-> 1
 1 << 1
 #-> 2
 1 << 2
 #-> 4
 1 << 3
 #-> 8
Say Thanks
Respond