Matt Todd
8/11/2006 12:30:00 AM
Should be just like so:
def [] index
# handle index or regexp or whatever
end
I know that it seems odd, but you basically just put the skeleton of
the method name in there as the name and then the parameters are what
go into the expression. You could override the + method (and many
others) as well. For instance:
class Foo
def + operand
# do some merging or something of whatever Foo is
end
end
In that way, you can define some weird magic to happen, like taking
two ActiveRecord models in and spitting out a relationship, for
instance:
(Post.find_by_id(12) + Tag.find_or_create_by_tag('Ruby')).save
We call #save on what's returned because it's a PostTag model that's returned...
Of course, this could be ironed out a bit, but you see the point:
overload operators with just their names... as simple as
def + operand
# magic here
end
Etc, etc, etc.
M.T.
(Sorry, I'm a bit drowsy, so this might not make total sense.)