# Operator overload on views

**URL:** <https://www.ruby-forum.com/t/operator-overload-on-views/59965>\
**Category:** Rails\
**Created:** [May 9, 2006, 1:42am UTC](https://www.ruby-forum.com/t/operator-overload-on-views/59965 "2006-05-09T01:42:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![malweth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@malweth](https://www.ruby-forum.com/u/malweth)\
**Post date:** [May 9, 2006, 1:42am UTC](https://www.ruby-forum.com/t/operator-overload-on-views/59965/1 "2006-05-09T01:42:19Z")

</div>

In my User auth scheme I have a userlevel. I’ve overloaded the User  
class operators like this:

def \>(item)  
if item.class == “User”  
self.userlevel \> item.userlevel  
else  
self.userlevel \> item  
end  
end

(other operators are overloaded in essentially the same way). As I  
understand it, using the operator “\>” inside the overloaded operator is  
OK because User.userlevel.class == Fixnum.

in my “edit.rhtml” view, I have the following code:

\<% if @local\_user \> @user &&  
@local\_user \>= 7000 ||  
@local\_user == @user %\>

…

\<% end %\>

this produces an exception:  
“comparison of Fixnum with User failed” - are my overloaded operators  
not being used, or am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![malweth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@malweth](https://www.ruby-forum.com/u/malweth)\
**Post date:** [May 9, 2006, 2:51am UTC](https://www.ruby-forum.com/t/operator-overload-on-views/59965/2 "2006-05-09T02:51:57Z")

</div>

Try changing your comparison:

if item.class == “User”

to

if item.class == User

On a side note, I’m not a fan of overloading these operators.

Dan.

---

<div class="post-metadata">

**Author:** ![malweth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@malweth](https://www.ruby-forum.com/u/malweth)\
**Post date:** [May 9, 2006, 3:22am UTC](https://www.ruby-forum.com/t/operator-overload-on-views/59965/3 "2006-05-09T03:22:36Z")

</div>

Thanks! I should have realized the class method returned a class 😉

Why don’t you believe in overloading these operators? In an activerecord  
class, I find it fairly unuseful to compare memory space pointers.  
Overloading the operators to compare the most useful information  
(userlevel in this case) seems to make sense.

---

<div class="post-metadata">

**Author:** ![malweth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cdc98d/32.png) [@malweth](https://www.ruby-forum.com/u/malweth)\
**Post date:** [May 9, 2006, 4:03am UTC](https://www.ruby-forum.com/t/operator-overload-on-views/59965/4 "2006-05-09T04:03:38Z")

</div>

I usually don’t like this kind of overloading because it’s not obvious  
what  
the overload means, which makes it less maintainable. I don’t naturally  
associate user levels with \< and \> operators - I’d just as likely  
associate  
user id comparisons to them. In this case, I’d prefer a named  
comparison  
function like is\_higher\_userlevel().

But, it’s all down to the coding style of your organisation. Good  
documentation can make this is a non-issue.

Cheers,

Dan.
