2

I have seen other used often in class comparisons, such as

def ==(other)
  ...
end

or

def eql?(other)
  self == other
end

but I still have found no explanation of what it actually is. What's going on here?

And perhaps this is for another question, but what does starting a method with == imply?

lusk
  • 541
  • 5
  • 21

5 Answers5

7

In ruby, operators are in fact method calls. If you have two variables a and b and want to check their equality, you generally write a == b, but you could write a.==(b). The last syntax shows what happens during an equality check : ruby calls a's method == and passes it b as an argument.

You can implement custom equality check in your classes by defining the == and/or the eql? methods. In your example, other is simply the name of the argument it receives.

class Person
     attr_accessor :name

    def initialize name
        @name = name
    end
end

a = Person.new("John")
b = Person.new("John")
a == b # --> false

class Person
    def == other
        name == other.name
    end
end
a == b # --> true

For your second question, the only methods starting with == you're allowed to implement are == and ===. Check here for the full list of restrictions on method names in ruby: What are the restrictions for method names in Ruby?

VonD
  • 5,075
  • 2
  • 20
  • 30
3

other is a parameter to this method, the object, that is being passed.

For example:

class A
  def ==(other)
    :lala == other
  end
end

obj = A.new
obj.==(:foo) # full syntax, just like any other method
# but there's also a shorthand for operators:
obj == :foo  # => false
obj == :lala # => true
Vasfed
  • 18,013
  • 10
  • 47
  • 53
  • Ahh, I see. I had been thinking that `other` must be a predefined object like `self` but in fact it's just a commonly-used parameter. – lusk Apr 11 '16 at 15:37
2

other is the parameter for == and it represents the object you are comparing with.

Example

x == y

The == method (yes, its just a method!), on your x object, gets called with y as a parameter.

Welcome to Ruby, you'll love it after a while :)

Len
  • 2,093
  • 4
  • 34
  • 51
  • I need to get a tattoo that reminds me that operators are methods, too! This keeps slipping my mind so your answer is very helpful. – lusk Apr 11 '16 at 15:38
  • Glad it helped. Put that tattoo anywhere but on your back ;) – Len Apr 12 '16 at 10:18
0

other, in this case, is the object you're comparing to, so this:

class SomeClass
  def ==(other)
    self == other
  end
end

means:

SomeClass.new == nil # here "other" is nil, this returns false

Basically def ==(other) is the implementation of the == operator, for cases where you do something specific in your class regarding comparison using ==.

Mario Carrion
  • 671
  • 5
  • 11
0

For numbers you can get the sum of 2 numbers using:

a= x + y

in Ruby everything is object, Right! so x and y are objects and for Number(Object) x it has a defined method + which accept 1 paramter which is y

same for what you are trying to understand, what if you have 2 classes and you want to check if they are equal or not and its your defined class and you want to specify how the are equal for example if their name attribute is equal then you can say:

class Student
  def ==(other)
   self.name == other.name
  end  
end

fi = Student.new(name: 'Mark')
sec = Student.new(name: 'Hany')
if (fi == sec)
  # do something here
mohamed-ibrahim
  • 10,837
  • 4
  • 39
  • 51