I'm trying to create a little Ruby hack to make something like the reverse of the Symbol#to_proc hack. Whereas the Symbol#to_proc hack makes this possible:
some_array.each(&:some_method)
is the same as
some_array.each { |obj| obj.some_method }
I want to make this possible:
some_array.each(&[:some_method])
would be the same as
some_array.each { |obj| some_method(obj) }
The problem is that unless some_method is a kernel method, what it would really mean is:
some_array.each { |obj| self.some_method(obj) }
When an block is passed explicitly, it's created within the binding where this line is written, so self correctly accesses the current object and everything works fine. However if the block is created in the Array's to_proc method, self is within the Array binding (and refers to the array itself). Perhaps it's not possible to do what I'm trying to do, but I'd love some ideas from the community.
Here's my hack so far. This works as long as the method is a kernel method, and is available within Array:
class Array
def to_proc
lambda {|i| send(self.first, i)}
end
end
With this hack, this line works, and puts the three symbols:
[:foo, :bar, :bas].each(&[:puts])
It's useful to me already, because often I'm finding myself wanting to inspect an array of ActiveRecord objects within the console and it saves me from typing { |i| puts i}. But it would be even more useful to be able to use it within objects so that some method within the object gets called with every item in the array passed as a parameter.
So any idea how to make the binding issue work?
Please don't both responding with "you don't want to do this because it'll make things run slower" or "you're not saving so many characters typing, but it makes the code less readable" -- I know these things already. :) This is more of a feeding my curiosity kind of hack. Just wondering if it's possible.