Table of contents
1.
Introduction
2.
What is Ordering?
2.1.
Properties of <=> Operator
2.2.
Examples
3.
Comparable Modules and Mixins
4.
Frequently Asked Questions
4.1.
What is Ruby?
4.2.
What are Objects in Ruby?
4.3.
What is a Class in Ruby?
4.4.
What is Ordering in Ruby?
4.5.
How do classes define ordering in Ruby?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Object Order In Ruby

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ruby is a general-purpose, dynamic, reflective, object-oriented programming language. Everything in Ruby is an object, except blocks, which have replacements in the form of procs and lambda. Ruby's development aimed to create a useful interface between human programmers and the underlying computational machinery.

Ruby's syntax is comparable to that of many other programming languages, such as C and Java, making learning simple for Java and C programmers. It works on a wide range of platforms, including Windows, Mac OS X, and Linux.

Let’s discuss about the object ordering in Ruby, what ordering is, and how it is done in Ruby.

What is Ordering?

Ordering: For any two instances of a class in ruby, the two instances must be equal, or one instance must be less than the other.

In Ruby, classes define an ordering by implementing the <=> operator.

The most obvious classes for which such an ordering is defined are Numbers. Strings are also sorted numerically according to the character codes that comprise the strings. (This is an approximate case-sensitive alphabetical order with the ASCII text.)

Class instances can be compared and sorted if the class defines an ordering.

Properties of <=> Operator

The main properties of <=> operator are:-

1.) Classes define ordering in Ruby by implementing the <=> operator. 

2.) If the left operand is smaller than the right operand, this operator should return -1.

3.) If the two operands are equal, this operator should return 0.

4.) If the left operand is greater than the right operand, this operator should return 1.

5.) And, if the two operands cannot be adequately compared (for example, if the right operand belongs to a different class), the operator should return nil.

Examples

1.) 15 <=> 20    # it will return -1 since 15 is less than 20.

2.) 25 <=> 25    # it will return 0 since 25 is equal to 25.

3.) 50 <=> 25    # it will return 1 since 50 is greater than 25.

4.) "10" <=> 15  # it will return nil since integers and strings are not comparable.

Comparable Modules and Mixins

To compare values, only the <=> operator is required. However, it isn't really intuitive.

As a result, most classes that define this operator include the Comparable module as a mixin.  

In terms of <=>, the Comparable mixin defines the following operators:-

1.) < :  Less than

2.) <= : Less than or equal

3.) == : Equal

4.) > : Greater than

5.) >= : Greater than or equal

The != operator is not defined in Comparable; Ruby automatically defines it as the negative of the == operator.

In addition to these comparison operators, Comparable additionally includes a helpful comparison method called between?.

For example, 10.between?(5, 15)   # it will return true since 10 lies between 5 and 15. 

All comparison operators derived from the <=> operator return false if the <=> operator returns nil. As an example, consider the special Float value NaN:

1.) nan < 0         # it will return false as nan is not less than zero.

2.) nan > 0         # it will return false as nan is greater than zero.

3.) nan == 0.      # it will return false as nan is not equal to zero.

4.) nan == nan   # it will return false as nan is not even equal to itself.

Frequently Asked Questions

What is Ruby?

Ruby is a general-purpose, dynamic, reflective, object-oriented programming language. Everything in Ruby is an object, except blocks, which have replacements in the form of procs and lambda.

What are Objects in Ruby?

In Ruby, everything is an object. All objects have a unique identification; they can also maintain a state and exhibit behavior in response to messages. Usually, these messages are sent out via method calls. A string is an example of a Ruby object.

What is a Class in Ruby?

In Ruby, classes are first-class objects, each being an instance of the class Class.

What is Ordering in Ruby?

Ordering in ruby is defined as, For any two instances of a class in ruby, the two instances must be equal, or one instance must be less than the other.

How do classes define ordering in Ruby?

In Ruby, classes define an ordering by implementing the <=> operator.

Conclusion

In this article, we have extensively discussed Object Ordering in Ruby, what order is, and how it is implemented in Ruby.

After reading about the object order in Ruby, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has covered it. To learn, see Object Marshalling in RubyTainting Objects in Ruby, and Object References in Ruby.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Coding!

 

Live masterclass