Comparison Operators - Ruby For Beginners

Previous Next Contents
  • Ruby For Beginners
  • Preface
  • Programming is creation
  • Learning to program
    • Learning modes
    • Don’t believe everything we say
    • Formatting your code
    • Reading error messages
    • Using Google
  • Your tools
    • Text Editor
    • Terminal
    • Ruby runtime
    • Programming workflow
    • Interactive Ruby
  • Our Roadmap
  • Object-oriented programming
  • Variables
    • Reusing variable names
    • Things on the right go first
  • Built-In Data Types
    • Numbers
    • Strings
    • True, False, and Nil
    • Symbols
    • Arrays
    • Hashes
  • Objects, Classes, Methods
    • Objects have classes
    • Classes create objects
    • Objects have methods
    • Calling methods
    • Passing arguments
    • Listing methods
    • Predicate methods
    • Bang Methods
  • Writing Methods
    • What makes a method
    • Defining a method
    • Using (calling) a method
    • Return values
    • Scopes
    • Combining Methods
    • Printing things
  • Writing classes
    • Defining classes
    • Defining instance methods
    • Initializing objects
    • Instance variables
    • Attribute readers
    • Attribute writers
    • State and behaviour
    • Interacting Objects
    • Object Scope and Self
  • Blocks
    • Alternative block syntaxes
    • Block arguments
    • Block return values
    • Inversion of control
    • Iterators
  • Conditionals
    • Nothingness and the truth
  • Operators
    • Arithmetical operators
    • Logical operators
    • Comparison operators
    • Operators are methods
  • Bonus Chapters
    • String interpolation
    • Top-level object
    • Lots of other methods
    • Questions and commands
    • Alternative Syntax
    • Using the right words
    • Arguments and parentheses
    • Terminology: Arguments vs Parameters
    • Writing a new method
  • Advanced Topics
    • Using Libraries (1)
    • Modules
    • Private methods
    • Procs
    • Yield
    • Regular Expressions
  • Exercises (old)
    • Working with Numbers
    • Working with Strings
    • Working with Arrays (1)
    • Working with Hashes (1)
    • Defining methods
    • Working with Arrays (2)
    • Working with Nested Arrays
    • Working with Hashes (2)
    • Truthiness
    • The Email Class
    • The Mailbox Class
    • The Mailbox Text Formatter
    • The Mailbox Html Formatter
    • Storing our HTML to a File
    • Reading from a CSV File
  • Exercises
    • Working with Numbers
    • Working with Strings
    • Working with Arrays (1)
    • Working with Hashes (1)
Comparison operators

In order to compare things Ruby has a bunch of comparison operators.

The operator == returns true if both objects can be considered the same. For example 1 == 1 * 1 will return true, because the numbers on both sides represent the same value. The expression "A" == "A" also returns true because both strings have the same value.

Likewise, two arrays are equivalent when they contain the same elements, in the same order. For example [1, 2] == [1, 2] will return true, but [1, 2] == [2, 3] and [1, 2] == [2, 1] both will return false.

Note that we say “considered the same” and “equivalent” because technically the two objects do not have to be (and most often, as in our examples) are not the same objects. E.g. while evaluating the expression "A" == "A" Ruby will actually create two different string objects which both contain a single character A.

In practice this is almost always what you want. For the rare case when you actually need to check if two objects are the same object there’s the method equal?. E.g., "A".equal?("A") returns false.

Other comparison operators are: less than < , less than or equal <=, greater than >, and greater than or equal >=. They also work on numbers and strings, in the way you’ll expect it. Open IRB and try a few combinations on numbers and strings.

Comparison operators most often are used in order to formulate conditions in if statements. Like so:

number = 20 puts "#{number} is greater than 10." if number > 10

The most funny operator in Ruby is <=>, because it’s called the spaceship operator. No kidding :) It is rather rarely used, and it is useful for implementing custom ways of sorting things.

Previous Next

Tag » How To Check For Value Greater Than 1 In Ruby