Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Point Equality in Ruby
3.
Difference Between eql? and equal?
4.
Duck Typing and Equality
5.
Frequently Asked Questions
5.1.
What is point equality in ruby?
5.2.
Is Ruby Set ordered?
5.3.
In Ruby, what do objects mean?
5.4.
In Ruby, is class an object?
5.5.
Is Ruby a computer language used for programming?
6.
Conclusion
Last Updated: Mar 27, 2024

Point Equality in Ruby

Author Shiva
0 upvote

Introduction

In this article, we’ve covered everything you need to know about point equality in ruby. Ruby is a high-level, general-purpose, interpreted programming language that supports a variety of paradigms.

ruby coding ninjas image

Point Equality in Ruby

Even if two separate Point instances have identical X and Y coordinates, they will never be equal to each other, that’s where point equality in ruby comes in. We need to offer an implementation of the == operator to fix this.

Point equality in ruby image

An == method for Point Equality in Ruby is as follows:

def ==(p)  					# Is self == p?
	if p.is_a? Point  		# to check if p is a Point object
		@x==p.x && @y==p.y  # compare parameters
	elsif  					# In case p is not a Point object
		false  				# then, self != p.
	end
end

 

Recall that Ruby objects also define an eql? method for determining equality.

The eql? method, like the == operator, checks object identity by default rather than object content equality. We can make eql? behave frequently like the == operator by giving it an alias:

class Point
	alias eql? ==
end

 

However, there are two situations in which we would wish eql? to differ from ==.

A stricter comparison than == is performed by some classes' definitions of eql? For instance, == permits type conversion in Numeric and its subclasses but eql? does not. We might use this example if we think that users of our Point class would need to be able to compare instances in two different ways. Points are essentially two integers, thus it seems logical to follow Numeric's lead in this instance. Similar to the == function, our eql? method compares point coordinates using eql? rather than ==:

def eql?(o)
	if o.instance_of? Point
		@x.eql?(o.x) && @y.eql?(o.y)
	elsif
		false
	end
end

 

Note that all classes that implement collections (sets, lists, or trees) of arbitrary objects should use this strategy. Both the == operator and the eql? method should compare the collection's members using their respective == operators and eql? Methods.

multiple equality in ruby

If you wish instances of your class to behave differently when used as a hash key, you should implement an eql? method that differs from the == operator. To compare hash keys, the Hash class makes use of eql? (but not values). Hashes will compare instances of your class according to object identity if you leave eql? undefined. In other words, if you assign a value to a key p, you can only retrieve that value from the same object p. Even if p == q, an object q won't function. Mutable objects are poor hash key candidates, but leaving eql? undefined neatly avoids the issue.

You must never use this method alone because eql? is used for hashes. To generate a hashcode for your object, you must specify a hash method if you define an eql? method. When two objects are considered equal by eql?, their hash methods must have the same result.

hash function

It can be challenging to implement the best hashing algorithms. Fortunately, there is a straightforward method to get exactly acceptable hashcodes for almost any class: just add the hashcodes of all the objects your class references. (To be more specific, combine the hashcodes of all the items your eql method compared.) Combining the hashcodes correctly is the trick. The hashing technique listed below is not recommended:

def hash
	@x.hash + @y.hash
end

 

Because this technique gives the same hashcode for the point (1, 0) as it does for the point, it contains a flaw (0,1). Although it is permitted, using points as hash keys results in below performance. Instead, let's change things up a little:

def hash
	code = 17
	code = 37*code + @x.hash
	code = 37*code + @y.hash
	# For each significant instance variable, add lines like this.
	code  # return the outcome's code.
end

 

This all-purpose hashcode formula ought to work with the majority of Ruby classes. It and its constants 17 and 37 were taken from Joshua Bloch's book Effective Java.

Difference Between eql? and equal?

eql? — Hash equality

If the hash keys for the objects obj and other are the same, the eql? function returns true. This is how Hash checks the equality of its members. The equivalent of eql? is == for objects of the class Object.

5 == 5.0     #=> true
5.eql? 5.0   #=> false

 

equal? — identity comparison

Due to the fact that it is used to determine object identity (i.e., if a and b are the same object, a.equal?(b)), the equal? method should never be modified by subclasses, in contrast to ==.

Duck Typing and Equality

Programming with a type-centric approach as opposed to a class-centric approach is referred to as "duck typing" in Ruby. The + operator, which we previously defined, does not perform any type checking at all and accepts any parameter object with methods x and y that return numeric values. Instead of permitting duck typing, this == method's implementation requires that the input be a Point.

equality in ruby

This is an option for implementation. The way that == is implemented above determines that an object cannot be equal to a Point unless it is also a Point.

Implementations could be more or less restrictive than this. The is_a? the predicate is used in the implementation above to check the argument's class. This makes it possible for an instance of a Point subclass to be equal to a Point. Instance_of? would be used in a stricter implementation to forbid instances of subclasses. Similar to the previous implementation, which compares the X and Y coordinates using ==. The point (5, 5) is equal to because the == operator allows type conversion for numbers (5.0, 5.0). This is most likely how it should be, although eql? might be used to compare the coordinates to a stronger definition of equality.

Duck typing might be acceptable if equality were defined more broadly. But some prudence is necessary. If the supplied object lacks the x and y methods, our == method shouldn't throw a NoMethodError. It should only return false as an alternative:

def ==(o) # Is self == o?
	@x == o.x && @y == o.y  		# Assuming that p has x and y methods
rescue  							# If the assumption is false
	false  						# Then self is not equal to p
end

Frequently Asked Questions

What is point equality in ruby?

Point Equality in Ruby refers to if there are 2 instances of a Point object; even if their values are the same. The 2 objects still won’t be equal.

Is Ruby Set ordered?

You can currently use Set as an ordered set because the Ruby 1.9 Set library is based on a Hash at the moment.

In Ruby, what do objects mean?

A Ruby object can be anything. All objects have an identity, the ability to maintain a state, and the capacity to exhibit behavior in response to signals. Typically, method calls are used to send these messages.

In Ruby, is class an object?

A Ruby class is an instance of the class Class, which includes all object-related objects as well as a list of methods and a reference to a superclass (which is itself another class). In Ruby, each method call specifies a recipient (which is by default self, the current object).

Is Ruby a computer language used for programming?

It's a very flexible programming language. Ruby programmers have the ability to alter the way the language functions. Instead of being compiled like C or C++, it is an interpreted language like Python.

Conclusion

In this article, we have discussed everything you need to know about Point Equality in Ruby. Here are more articles to the rescue:

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSACompetitive ProgrammingJavaScriptSystem Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.
Do upvote our blog to help other ninjas grow.
Happy Learning!

thank-you__coding-ninjas

Live masterclass