0x18 – More magic symbols

With just that small addition:


class Symbol
  alias_method :old_eeq, :"===" 

  def ===(obj)
    (obj.respond_to?(self) && obj.send(self)) ||
    old_eeq(obj)
  end
end

you can do things like


case current_user
when :admin?    then "admin"
when :regular?  then "something_for_regulars"
when :guest?   then "do_guest_stuuff"
else "huh"
end

Isn’t that cool much more readable?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s