How does Ruby gets bindings?

Discussion in 'Ruby' started by Rubén Medellín, Jul 15, 2007.

  1. 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.
     
    Rubén Medellín, Jul 15, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    5
    Views:
    527
    Steven Saunderson
    Jul 10, 2006
  2. fjodor
    Replies:
    0
    Views:
    140
    fjodor
    Jan 18, 2005
  3. John Joyce

    gets gets

    John Joyce, Mar 26, 2007, in forum: Ruby
    Replies:
    2
    Views:
    355
    John Joyce
    Mar 26, 2007
  4. John Joyce

    Return of gets gets

    John Joyce, Apr 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    196
    John Joyce
    Apr 23, 2007
  5. libsfan01
    Replies:
    5
    Views:
    246
    Jeff North
    Dec 20, 2006
Loading...

Share This Page