# Confused about != operator overloading

**URL:** <https://www.ruby-forum.com/t/confused-about-operator-overloading/54371>\
**Category:** Ruby\
**Created:** [March 2, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/confused-about-operator-overloading/54371 "2006-03-02T20:28:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Chris\_Parker](https://www.ruby-forum.com/u/Chris_Parker)\
**Post date:** [March 2, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/confused-about-operator-overloading/54371/1 "2006-03-02T20:28:46Z")

</div>

Hi,

I want to overload the != operator. I can overload the == operator. I  
get this error when I try to run the code without commenting out the !=  
operator overloading function:

CSP.rb:72: syntax error  
def !=(other)

The actual code is simply:

def !=(other)

end

Does Ruby not allow overloading !=? Does Ruby just give you != from  
==? This is a bit of a problem for my case because while != is the  
opposite of ==, there is a significant time improvement for stopping as  
soon as possible for !=.

Any help would be greatly appreciated.

Chris

---

<div class="post-metadata">

**Author:** ![Chris\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Chris\_Parker](https://www.ruby-forum.com/u/Chris_Parker)\
**Post date:** [March 2, 2006, 8:29pm UTC](https://www.ruby-forum.com/t/confused-about-operator-overloading/54371/2 "2006-03-02T20:29:09Z")

</div>

Chris Parker wrote:

> Chris

!= operator will use the == one

lopex

---

<div class="post-metadata">

**Author:** ![Chris\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Chris\_Parker](https://www.ruby-forum.com/u/Chris_Parker)\
**Post date:** [March 2, 2006, 10:56pm UTC](https://www.ruby-forum.com/t/confused-about-operator-overloading/54371/3 "2006-03-02T22:56:30Z")

</div>

Chris,

Ruby derives the != operator from the == operator. You can’t define  
them separately.

But, more to the point, can you give some more details on what you’re  
testing for equality/inequality? I’m fascinated to know what sort of  
beast can be checked for inequality faster than for equality.

Cheers,

Pete Y.

---

<div class="post-metadata">

**Author:** ![Chris\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Chris\_Parker](https://www.ruby-forum.com/u/Chris_Parker)\
**Post date:** [March 2, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/confused-about-operator-overloading/54371/4 "2006-03-02T23:29:45Z")

</div>

On Mar 2, 2006, at 2:28 PM, Chris Parker wrote:

> soon as possible for !=.
> 
> Any help would be greatly appreciated.
> 
> Chris

Is this fast enough?

def not\_equal(other)  
# optimized != that stops early  
end

def ==(other)  
not (not\_equal(other))  
end

so != gets turned into not (not (not\_equal(other))) which is not  
perfect but the “nots” should be relatively trivial

---

<div class="post-metadata">

**Author:** ![Chris\_Parker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@Chris\_Parker](https://www.ruby-forum.com/u/Chris_Parker)\
**Post date:** [March 3, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/confused-about-operator-overloading/54371/5 "2006-03-03T18:31:51Z")

</div>

Thanks for the replies everyone.

It is helpful but kind of disappointing. I’ll explain more about my  
problem and maybe there is some clean solution.

So I am trying to write a new language on top of Ruby (without writing  
any C) for Constraint Satisfaction Problems.

X1 == X2, which X1 is of class CSPVar will return a CSPConstraint that  
contains the information about that constraint. Obviously, I need to  
overload != because returning something like true or false makes no  
sense. I am using != needs to return a CSPConstraint object reflecting  
the information about the constraint, not true or false. After I have  
all of the variables, domains, and constraints, I then solve the  
problem.

The obvious but incredibly ugly solution is something like: X1.ne(X2).  
Since I got ==, +, -, \*, / all working like operators, it would be nice  
to get != to work as well.

Let me know if you have any ideas. Also, I am really struggling to  
understand why != can’t be overloaded. I understand providing a  
default != which is just not(==), but completely not allowing the  
overloading of != seems strange.

Chris
