Late binding Method

J

James Coglan

[Note: parts of this message were removed to make it a legal post.]

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top