# Help with UnboundMethod#bind error

**URL:** <https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617>\
**Category:** ruby-talk\
**Created:** [29 June 2003 15:31 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617 "2003-06-29T15:31:00Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriele\_renzi4](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@gabriele\_renzi4](https://rubytalk.org/u/gabriele_renzi4)\
**Post date:** [29 June 2003 15:31 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/1 "2003-06-29T15:31:00Z")

</div>

Hi gurus and nubys,

I’m having a little problem with unbound methods.  
I supposed I could unbound a method from Kernel  
and put it in my own module, but it seems’ I’m failing:

# this should go in Kernel

def met  
p ‘met’+self.to\_s  
end

m=Kernel.method( :met).unbind  
module Mod;end

m.bind(Mod) #failing!  
#but M.kind\_of?(Kernel.class) #=\>true

the error is :  
Ruby1:13:in `bind’: singleton method called for a different  
object(TypeError)

now: I know that Mod is a different object. Anyway MEthod#bind should  
worry just about obj.class, am I wrong ?

Or the problem is that :met is a singleton method ?  
If so, why I can’t detach a singleton method ?

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [29 June 2003 16:43 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/2 "2003-06-29T16:43:03Z")

</div>

Hi,

> I’m having a little problem with unbound methods.  
> I supposed I could unbound a method from Kernel  
> and put it in my own module, but it seems’ I’m failing:

Although I’m not sure what you really want, try instance\_method  
instead.

> m=Kernel.method( :met).unbind  
> m=Kernel.instance\_method(:met)

> now: I know that Mod is a different object. Anyway MEthod#bind should  
> worry just about obj.class, am I wrong ?

Modules and classes always have metaclasses.

It might be ok to ignore metaclasses have no influence.

Index: eval.c

> **···**
>
> # At Mon, 30 Jun 2003 00:31:00 +0900, gabriele renzi wrote:
> 
> RCS file: /cvs/ruby/src/ruby/eval.c,v  
> retrieving revision 1.470  
> diff -u -2 -p -r1.470 eval.c  
> — eval.c 28 Jun 2003 03:29:00 -0000 1.470  
> +++ eval.c 29 Jun 2003 16:34:32 -0000  
> @@ -7269,4 +7269,8 @@ mnew(klass, obj, id, mklass)  
> }
> 
> - while (rklass != klass &&
> - 
> 
> ```
> (FL_TEST(rklass, FL_SINGLETON) || TYPE(rklass) == T_ICLASS)) {
> 
> ```
> 
> - rklass = RCLASS(rklass)-\>super;
> - }  
> if (TYPE(klass) == T\_ICLASS) klass = RBASIC(klass)-\>klass;  
> method = Data\_Make\_Struct(mklass, struct METHOD, bm\_mark, free, data);
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![gabriele\_renzi4](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@gabriele\_renzi4](https://rubytalk.org/u/gabriele_renzi4)\
**Post date:** [29 June 2003 18:31 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/3 "2003-06-29T18:31:34Z")

</div>

> Although I’m not sure what you really want, try instance\_method  
> instead.

I was wondering of a quick way to decorate a method:

- unbind the method my\_met() from its current object  
(actually, I was thinking about method defined out of class  
definitions, ‘function’-like )
- put my\_met() in a different module
- define a new my\_met() using the old one.

> > m=Kernel.method( :met).unbind  
> > m=Kernel.instance\_method(:met)

well, I just thought the same thing,  
I had the thing working in this way:

> > def met  
> > puts ‘bau’  
> > end  
> > =\> nil  
> > m=Object.instance\_method(:met)  
> > =\> #\<UnboundMethod: Object#met\>  
> > module M  
> > end  
> > =\> nil  
> > m.bind(M)  
> > =\> #\<Method: Module(Object)#met\>

If I try Kernel.instance\_method I have a NameError.

> > now: I know that Mod is a different object. Anyway MEthod#bind should  
> > worry just about obj.class, am I wrong ?
> 
> Modules and classes always have metaclasses.

emh… yeah I should have remembered this…

> It might be ok to ignore metaclasses have no influence.

cool, that’s the first time that I get a patch just for my own purpose  
🙂

thank you very much

PS  
anyway, my idea fails, it would have been to cool if it worked.

If I try to redefine my\_met() as a “function” it  
gets propagated to MyModule, and I fall in a ‘stack level too deep’  
error.

I suppose I need a freeze-like feature to avoid MyModule from  
inherithing Kernel for a while… and then inherithing it again…  
Ok, I suppose I just need to sleep…

> **···**
>
> il Mon, 30 Jun 2003 01:43:03 +0900, [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) ha scritto::

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [30 June 2003 08:18 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/4 "2003-06-30T08:18:43Z")

</div>

Hi,

> **···**
>
> In message “Re: Help with UnboundMethod#bind error” on 03/06/30, [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) writes:
> 
> > It might be ok to ignore metaclasses have no influence.
> 
> Check in your change, please.
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [2 July 2003 07:52 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/5 "2003-07-02T07:52:59Z")

</div>

Though I read all messages in this thread, I’m not sure what Nobu’s  
patch will change. But since I had some problems re-binding unbound  
methods lately I’m interested in this topic. So could someone try to  
explain it to me?

Thanks in advance,  
Pit

> **···**
>
> On 30 Jun 2003 at 17:18, Yukihiro Matsumoto wrote:
> 
> > Check in your change, please.

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [2 July 2003 08:14 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/6 "2003-07-02T08:14:04Z")

</div>

Hi,

> Though I read all messages in this thread, I’m not sure what Nobu’s  
> patch will change.

A Method unbound from an object which has a signleton method  
couldn’t be bound to another object which belongs to same  
class.

> But since I had some problems re-binding unbound  
> methods lately I’m interested in this topic. So could someone try to  
> explain it to me?

What problem?

> **···**
>
> At Wed, 2 Jul 2003 16:52:59 +0900, Pit Capitain wrote:
> 
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [5 July 2003 08:58 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/7 "2003-07-05T08:58:21Z")

</div>

When overriding a method, we normally use alias to save the old  
implementation under a new name, so that we can call it later. People  
are trying to find obscure names for the aliases to avoid naming  
conflicts.

I was trying to find a way to override a method without having to  
find a unique name. Instead I wanted to be able to simply call the  
old implementation with “super”.

This is a simplified version of what I got (in Ruby 1.8):

class Class  
def override( name )  
mod = Module.new  
mod.send( :define\_method, name, instance\_method( name ) )  
include mod  
end  
end

As you can see, this creates a module, “copies” the original method  
into the module, and finally includes the module. Now you can call  
the original implementation via “super”:

class C  
def m  
puts “original”  
end  
end

C.new.m # =\> original

class C  
override :m  
def m  
puts “before(1)”  
super  
end  
end

C.new.m # =\> before(1), original

So far, so good. But if you do this one more time, you get:

class C  
override :m  
def m  
puts “before(2)”  
super  
end  
end

C.new.m # =\> before(2), before(1), before(1), …

So it seems that the methods which are moved into the new modules  
still “remember” that they originally were defined in class C, and  
when they call super they somehow restart the search in the first  
ancestor of C, not in the first ancestor of the module they are  
currently defined in.

I haven’t had time yet to look further into this, but maybe you know  
what’s going on here.

Regards,  
Pit

> **···**
>
> On 2 Jul 2003 at 17:14, [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) wrote:
> 
> > > But since I had some problems re-binding unbound  
> > > methods lately I’m interested in this topic. So could someone try to  
> > > explain it to me?
> > 
> > What problem?

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [5 July 2003 11:45 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/8 "2003-07-05T11:45:08Z")

</div>

> I was trying to find a way to override a method without having to  
> find a unique name. Instead I wanted to be able to simply call the  
> old implementation with "super".

Don't search what you want is called hook ...

svg% cat b.rb  
#!./ruby  
class C  
&nbsp;&nbsp;&nbsp;def m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "original"  
&nbsp;&nbsp;&nbsp;end  
end

puts "====================="  
C.new.m  
puts "====================="

module M  
&nbsp;&nbsp;&nbsp;def m:before  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "before(1)"  
&nbsp;&nbsp;&nbsp;end  
end

class C  
&nbsp;&nbsp;&nbsp;include M  
end

C.new.m  
puts "====================="

class C  
&nbsp;&nbsp;&nbsp;def m:before  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "before(2)"  
&nbsp;&nbsp;&nbsp;end  
end

C.new.m  
puts "====================="  
svg%

svg% b.rb

> **···**
>
> # ===================== original
> 
> # before(1) original
> 
> # before(2) before(1) original
> 
> svg%
> 
> Guy Decoux

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [6 July 2003 07:39 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/9 "2003-07-06T07:39:06Z")

</div>

Hi Guy,

yes, I know that you hacked Ruby and that Matz is also working on it,  
but for me using the PragProg Ruby installer on Windows nothing of  
those I can use right now.

So before I end my search you or Matz would have to release your code  
🙂

Regards,  
Pit

> **···**
>
> On 5 Jul 2003 at 20:45, ts wrote:
> 
> > Don’t search what you want is called hook …

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [6 July 2003 12:23 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/10 "2003-07-06T12:23:28Z")

</div>

> yes, I know that you hacked Ruby and that Matz is also working on it,  
> but for me using the PragProg Ruby installer on Windows nothing of  
> those I can use right now.

Write an extension :

svg% cat b.rb  
#!/usr/bin/ruby  
require 'ovr'

class C  
&nbsp;&nbsp;&nbsp;def m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "original"  
&nbsp;&nbsp;&nbsp;end  
end

C.new.m # =\> original

class C  
&nbsp;&nbsp;&nbsp;override :m  
&nbsp;&nbsp;&nbsp;def m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "before(1)"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;end  
end

C.new.m # =\> before(1), original

class C  
&nbsp;&nbsp;&nbsp;override :m  
&nbsp;&nbsp;&nbsp;def m  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "before(2)"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;end  
end

C.new.m # =\> before(2), before(1), before(1), ...  
svg%

svg% b.rb  
original  
before(1)  
original  
before(2)  
before(1)  
original  
svg%

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [6 July 2003 14:51 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/11 "2003-07-06T14:51:04Z")

</div>

This is the real Guy Decoux we all love and respect!

I know you can do it and maybe I would be able to do so, too, after  
spending enough time on it. Thank you for trying to motivate me to  
come up with my own solution. But I’m sure this won’t happen. So I’ll  
be waiting for Matz’s code. Maybe Nobu will find some time to tell me  
why my pure Ruby solution doesn’t work.

Regards,  
Pit

> **···**
>
> On 6 Jul 2003 at 21:23, ts wrote:
> 
> > > yes, I know that you hacked Ruby and that Matz is also working on it,  
> > > but for me using the PragProg Ruby installer on Windows nothing of  
> > > those I can use right now.
> > 
> > Write an extension :
> > 
> > (… demo of Guy’s secret code in action …)

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [6 July 2003 17:03 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/12 "2003-07-06T17:03:42Z")

</div>

The extension is easy to write (although you’ll have to wait for Guy to  
check if my code is a time bomb :-):

override.c:

> **···**
>
> On Sun, Jul 06, 2003 at 11:51:04PM +0900, Pit Capitain wrote:
> 
> > On 6 Jul 2003 at 21:23, ts wrote:
> 
> > > > yes, I know that you hacked Ruby and that Matz is also working on it,  
> > > > but for me using the PragProg Ruby installer on Windows nothing of  
> > > > those I can use right now.
> > > 
> > > Write an extension :
> > > 
> > > (… demo of Guy’s secret code in action …)
> > 
> > This is the real Guy Decoux we all love and respect!
> > 
> > I know you can do it and maybe I would be able to do so, too, after  
> > spending enough time on it. Thank you for trying to motivate me to  
> > come up with my own solution. But I’m sure this won’t happen. So I’ll  
> > be waiting for Matz’s code. Maybe Nobu will find some time to tell me  
> > why my pure Ruby solution doesn’t work.
> 
> ===================  
> #include \<ruby.h\>  
> #include \<st.h\>  
> #include \<node.h\>
> 
> static  
> VALUE  
> class\_override(VALUE self, VALUE symbols)  
> {  
> int len;  
> int i;  
> VALUE meth;  
> VALUE name;  
> st\_data\_t data;
> 
> NEWOBJ(proxy, struct RClass);  
> OBJSETUP(proxy, rb\_cClass, T\_ICLASS);
> 
> len = RARRAY(symbols)-\>len;
> 
> proxy-\>iv\_tbl = st\_init\_numtable(); /\* not needed? \*/  
> proxy-\>m\_tbl = st\_init\_numtable();  
> proxy-\>super = RCLASS(self)-\>super;
> 
> RBASIC(proxy)-\>klass = RBASIC(self)-\>klass;
> 
> for(i = 0; i \< len; i++) {  
> meth = rb\_ary\_entry(symbols, i);  
> name = rb\_funcall(meth, rb\_intern(“to\_s”), 0);  
> if(!st\_lookup(RCLASS(self)-\>m\_tbl, rb\_intern(RSTRING(name)-\>ptr), &data))  
> continue; /\* should do something else?? \*/  
> st\_insert(proxy-\>m\_tbl, rb\_intern(RSTRING(name)-\>ptr), data);  
> }
> 
> RCLASS(self)-\>super = proxy;  
> OBJ\_INFECT(proxy, self);
> 
> return self;  
> }
> 
> # void Init\_override() { rb\_define\_method(rb\_cClass, “override”, class\_override, -2); }
> 
> # extconf.rb:
> 
> # require ‘mkmf’ create\_makefile(“override”)
> 
> And then…
> 
> batsman@tux-chan:~/src/override$ expand -t2 b.rb  
> #!/usr/bin/ruby  
> require ‘override’
> 
> class C  
> def m  
> puts “original”  
> end  
> end
> 
> C.new.m # =\> original
> 
> class C  
> override :m  
> def m  
> puts “before(1)”  
> super  
> end  
> end
> 
> C.new.m # =\> before(1), original
> 
> class C  
> override :m  
> def m  
> puts “before(2)”  
> super  
> end  
> end
> 
> C.new.m # =\> before(2), before(1), original  
> batsman@tux-chan:~/src/override$ ruby b.rb  
> original  
> b.rb:14: warning: method redefined; discarding old m  
> before(1)  
> original  
> b.rb:24: warning: method redefined; discarding old m  
> before(2)  
> before(1)  
> original
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Anyone want the new supermount? 🙂  
> whats new aboutit  
> klogd: It cleans whiter than white. 🙂  
> – Seen on #Linux

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [6 July 2003 17:18 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/13 "2003-07-06T17:18:42Z")

</div>

Quick fix for the warnings attached:

batsman@tux-chan:~/src/override$ expand -t2 b.rb  
#!/usr/bin/ruby  
require ‘override’

class C  
def m  
puts “original”  
end  
end

C.new.m # =\> original

puts “=” \* 20  
class C  
override :m  
def m  
puts “before(1)”  
super  
puts “after(1)”  
end  
end

C.new.m # =\> before(1), original, after(1)

puts “=” \* 20  
class C  
override :m  
def m  
puts “before(2)”  
super  
puts “after(2)”  
end  
end

C.new.m # =\> before(2), before(1), original, after(1), after(2)  
batsman@tux-chan:~/src/override$ ruby b.rb  
original

[override.c](https://rubytalk.org/uploads/short-url/6opM5pJYgGN4Kn5H8aHc9BBDgFq.c) (974 Bytes)

> **···**
>
> On Mon, Jul 07, 2003 at 02:03:42AM +0900, Mauricio Fernández wrote:
> 
> > On Sun, Jul 06, 2003 at 11:51:04PM +0900, Pit Capitain wrote:
> > 
> > > On 6 Jul 2003 at 21:23, ts wrote:
> 
> > > > > yes, I know that you hacked Ruby and that Matz is also working on it,  
> > > > > but for me using the PragProg Ruby installer on Windows nothing of  
> > > > > those I can use right now.
> > > > 
> > > > Write an extension :
> > > > 
> > > > (… demo of Guy’s secret code in action …)
> > > 
> > > This is the real Guy Decoux we all love and respect!
> > > 
> > > I know you can do it and maybe I would be able to do so, too, after  
> > > spending enough time on it. Thank you for trying to motivate me to  
> > > come up with my own solution. But I’m sure this won’t happen. So I’ll  
> > > be waiting for Matz’s code. Maybe Nobu will find some time to tell me  
> > > why my pure Ruby solution doesn’t work.
> > 
> > The extension is easy to write (although you’ll have to wait for Guy to  
> > check if my code is a time bomb :-):
> 
> # ==================== before(1) original after(1)
> 
> before(2)  
> before(1)  
> original  
> after(1)  
> after(2)
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> No, that’s wrong too. Now there’s a race condition between the rm and  
> the mv. Hmm, I need more coffee.  
> – Guy Maor on Debian Bug#25228

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [7 July 2003 10:54 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/14 "2003-07-07T10:54:29Z")

</div>

> The extension is easy to write (although you'll have to wait for Guy to  
> check if my code is a time bomb :-):

Well, I've sent in a private email the extension to "Pit Capitain".

For me it has some problems :

&nbsp;&nbsp;1) you must know where a method is defined when you call ::override

&nbsp;&nbsp;2) it work actually for Class and not Module, it's easy to change it but  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;you'll have the same problem than in [ruby-talk:72510]

Imagine that you want to intercept the method #each for all Enumerable :  
instinctively you write

&nbsp;&nbsp;&nbsp;module Enumerable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overrive :each  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def each  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "before each"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

Then you write

&nbsp;&nbsp;&nbsp;[1, 2].each {|i| p i}

and you'll see that it don't work because Array has defined #each

You don't have these problems the way that matz do it, because you just  
write

svg% cat b.rb  
#!./ruby  
module Enumerable  
&nbsp;&nbsp;&nbsp;def each:before  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "before each"  
&nbsp;&nbsp;&nbsp;end  
end

[1, 2].each {|i| p i }  
{"a" =\> 1, "b" =\> 2}.each {|\*i| p i }  
(1..2).each {|i| p i }  
svg%

svg% b.rb  
before each  
1  
2  
before each  
["a", 1]  
["b", 2]  
before each  
1  
2  
svg%

You just need to know the relation that it exist between classes (or  
classes and modules) and you have this information. Rather than know where  
a method is defined (information that you don't have fatally).

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [6 July 2003 22:43 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/15 "2003-07-06T22:43:28Z")

</div>

Hi,

rb\_to\_id() will help.

> **···**
>
> In message “Re: Help with UnboundMethod#bind error” on 03/07/07, Mauricio Fernández [batsman.geo@yahoo.com](mailto:batsman.geo@yahoo.com) writes:
> 
> > ID name;
> 
> > for(i = 0; i \< len; i++) {  
> > name = rb\_to\_id(rb\_ary\_entry(symbols, i));  
> > if(!st\_lookup(RCLASS(self)-\>m\_tbl, name, &data))  
> > continue; /\* should do something else?? \*/  
> > st\_insert(proxy-\>m\_tbl, name, data);  
> > –  
> > matz.

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [7 July 2003 05:48 UTC](https://rubytalk.org/t/help-with-unboundmethod-bind-error/6617/16 "2003-07-07T05:48:14Z")

</div>

Thanks.

Is otherwise the code correct? I see two possible problems, but I’m not  
sure about them:

- I believe just copying the NODE\* is OK, but rb\_alias makes a new one  
NEW\_METHOD(NEW\_FBODY(body, def, origin), orig-\>nd\_noex)  
NODEs are GCed like any object so just copying should be  
safe, but I’m not really sure
- is rb\_clear\_cache\_by\_name necessary after adding the proxy class?

> **···**
>
> On Mon, Jul 07, 2003 at 07:43:28AM +0900, Yukihiro Matsumoto wrote:
> 
> > Hi,
> > 
> > rb\_to\_id() will help.
> > 
> > In message “Re: Help with UnboundMethod#bind error” \> on 03/07/07, Mauricio Fernández [batsman.geo@yahoo.com](mailto:batsman.geo@yahoo.com) writes:
> > 
> > > ID name;
> > 
> > > for(i = 0; i \< len; i++) {  
> > > name = rb\_to\_id(rb\_ary\_entry(symbols, i));  
> > > if(!st\_lookup(RCLASS(self)-\>m\_tbl, name, &data))  
> > > continue; /\* should do something else?? \*/  
> > > st\_insert(proxy-\>m\_tbl, name, data);
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> And Bruce is effectively building BruceIX  
> – Alan Cox
