Forum: Ruby How does Ruby gets bindings?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F2c730957ef0aec5616091b30fe88075?d=identicon&s=25 Ruben Medellin (chubas)
on 2007-07-15 18:56
(Received via mailing list)
Hi everyone.
Playing with modules, I come to the following:


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

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

module M
  define_method( :baz, N.instance_method(:bar) )  #Evil!
end    # TypeError: bind argument must be an instance of N
                    # Everything works as expected. But...

M.instance_method( :baz ).bind( ).call
# => TypeError. bind argument must be an instance of N
M.instance_method( :baz ).bind( ).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.
This topic is locked and can not be replied to.