Comparing Values in Ruby

==

== 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

We use cookies and similar technologies to enhance the quality of services, maintain statistics and adjust marketing content. You will find more information in the Cookies Policy.

By clicking OK you grant consent to processing of your personal data by us and our Trusted Partners with the purpose of maintain statistics and adjustment of the marketing content pursuant to the Privacy Policy. If you wish to not grant that consent and/or limit its extent click Settings.