Forum: Ruby on Rails operator overload on views

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
33b94e9f3d395fecb2d221b002dda739?d=identicon&s=25 Zeke Tamayo (malweth)
on 2006-05-09 01:42
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?
9e44fa55e7078bff7c3dab8404913928?d=identicon&s=25 Dan Venkitachalam (Guest)
on 2006-05-09 02:51
(Received via mailing list)
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.
33b94e9f3d395fecb2d221b002dda739?d=identicon&s=25 Zeke Tamayo (malweth)
on 2006-05-09 03:22
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.
9e44fa55e7078bff7c3dab8404913928?d=identicon&s=25 Dan Venkitachalam (Guest)
on 2006-05-09 04:03
(Received via mailing list)
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.
This topic is locked and can not be replied to.