Skip to Main Content
Learn to Program, 2nd Edition
book

Learn to Program, 2nd Edition

by Chris Pine
March 2009
Intermediate to advanced content levelIntermediate to advanced
194 pages
4h
English
Pragmatic Bookshelf
Content preview from Learn to Program, 2nd Edition

Comparison Methods

You’re getting good at this, so I’ll try to let the code do the talking. First, to see whether one object is greater than or less than another, we use the methods > and <:

puts 1 > 2
puts 1 < 2
false
true

No problem.

Likewise, we can find out whether an object is greater than or equal to another (or less than or equal to) with the methods >= and <=:

puts 5 >= 5
puts 5 <= 4
true
false

And finally, we can see whether two objects are equal using == (which means “Are these equal?”) and != (which means “Are these different?”). It’s important not to confuse = with ==. = is for telling a variable to point at an object (assignment), and == is for asking the question “Are these two objects equal?”

puts 1 == 1
puts 2 != 1
true
true

Of course, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn to Program, 3rd Edition

Learn to Program, 3rd Edition

Chris Pine
The Book of Ruby

The Book of Ruby

Huw Collingbourne

Publisher Resources

ISBN: 9781680500172Errata