How does Ruby gets bindings?

R

Rubén Medellín

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.
 

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,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top