[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How does Ruby gets bindings?

Rubén Medellín

7/15/2007 4:51:00 PM

Hi everyone.
Playing with modules, I come to the following:

#------------------------------

module M
def foo; end
end
module N
def bar; end
end

class C; include M; end
class D; include N; end

module M
define_method( :baz, N.instance_method(:bar) ) #Evil!
end

C.new.baz # TypeError: bind argument must be an instance of N
# Everything works as expected. But...

M.instance_method( :baz ).bind( C.new ).call
# => TypeError. bind argument must be an instance of N
M.instance_method( :baz ).bind( D.new ).call
# => TypeError.bind argument must be an instance of M

#--------------------------------

I wonder how bindings are set, if dynamically when the method is
called or since the method is defined.
AFAIK, it's impossible to change the binding of an Unbound method
(except for Evil Ruby, but I'm not that evil). Anyways, I find
somewhat weird this behavior.