The madness continues! This is getting out of hand!
First, a recap:
class Symbol
def to_proc
# leave out the `&block’ unless
# you’re using Ruby 1.9
proc{|obj, *args, &block| obj.send(self, *args, &block) }
end
end
now we can do stuff like this:
%w{dog cat monkey}.collect(&:upcase) => [“DOG”, “CAT”, “MONKEY”]
this is also mainly 1.9 stuff
module Enumerable
class Enumerator
def &(name)
each(&name)
end
end
end
This is cool, but it looks kind of weird:
%w{cat dog monkey}.collect&:reverse => [“tac”, “god”, “yeknom”]
And here’s the new stuff:
now, this is really, really stupid in a real-world app.
but hey, it’s still pretty cool!
def method_missing(name, *args)
return name
end
now just look at all the coolness!
%w{dog cat monkey}.collect&upcase => [“DOG”, “CAT”, “MONKEY”]
You can even put spaces in!
%w{dog cat monkey}.collect & reverse => [“tac”, “god”, “yeknom”]
this will of course break if the variable is defined
upcase = “foobar”
%w{dog cat monkey}.collect & upcase => TypeError
I can’t believe I used to write in PHP!
Cheers,
Daniel