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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top