Hello all,
I was just wondering whether it’s at all useful to have a late-binding
version of Object#method? I’m more concerned about this in the context
of
JS.Class, my JavaScript implementation of Ruby’s object system – I tend
to
use functional programming with object methods way more in JavaScript. I
hacked this up:
class Method
attr_writer :name
alias call_without_late_binding call
def [](*args, &block)
args.unshift(@name) if @name
call_without_late_binding(*args, &block)
end
def call(*args, &block)
args.unshift(@name) if @name
call_without_late_binding(*args, &block)
end
end
class Object
def late_bound(name)
m = method :__send__
m.name = name
m
end
end
I really wnated to subclass Method instead but I couldn’t get that to
work.
So we get this:
class Foo
def talk; 'foo'; end
end
f = Foo.new
puts f.talk # => 'foo'
m = f.method :talk
puts m[] # => 'foo'
l = f.late_bound :talk
puts l[] # => 'foo'
class Foo
def talk; 'bar'; end
end
puts f.talk #=> 'bar'
puts m[] #=> 'foo'
puts l[] #=> 'bar'
Any thoughts?