Comparing Values

==

== method is implemented by all the aforementioned built-in Ruby objects. It is used to compare them and evaluates to true or false. Generally, the method does not care whether the objects are equal but whether their values are the same. Therefore, comparing with == two different string objects with the same values gives true.

1 == 1 # => true
"ruby" == "ruby" # => true
1 == 2 # => false
"ruby" == "javascript" # => false

!=

!= method is implemented by all the aforementioned built-in Ruby objects. Similarly to ==, the method takes into account the objects' values and is used to compare them. It evaluates to true when the objects' values differ and to false when the objects' values are the same.

1 != 1 # => false
"ruby" != "ruby" # => false
1 != 2 # => true
"ruby" != "javascript" # => true

Comparing Numbers

In addition to comparing numbers with == and != we can also compare them with < (less than), > (more than), <= (less than or equal to) and >= (more than or equal to).

1 < 2 # => true
1 <= 1 # => true