From: James Coglan Date: 2008-07-02T19:56:25+09:00 Subject: Late binding Method ------=_Part_1167_3573017.1214996365399 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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? -- James Coglan http://blog.jcoglan.com ------=_Part_1167_3573017.1214996365399--