From: E S Date: 2005-01-26T07:51:57+09:00 Subject: Re: Injecting methods from one class into another. > L�hett�j�: "George Moschovitis" > Aihe: Re: Injecting methods from one class into another. > > > I do not think what you want has anything to do with 'duck typing'. > > Of course it has. Have a look at the following example: > > class C1 > attr_accessor :a > > def a1 > puts @a > end > > def a2 > puts 'hello' > end > end > > class C2 > attr_accessor :a > end > > So for method C1#a1 the class C2 'quacks like a duck' (ie, it has the > attribute @a) > > So in 'duck typing' spirit, it should be possible to do: > > c = C2.new > > m = C1.instance_method(:a1) > m.bind(c) > > c.a1 > > however Ruby raises a TypeError :( You're forgetting something. It's not "If it quacks...", it's "If it quacks... and if it walks...". The gist is that as long as an object has *all* of the (required) characteristics of a type, the object can be treated as a specimen of that type. As an animal analogy regarding 'injecting' methods, tearing off the wings of a duck and gluing them to a pig doesn't mean the pig can fly. > Can anyone give me an example, where this #bind method is usefull? I > mean, you can only bind a Method back to an object of the original > method's Class or a Subclass. But such an object allready has this > method, so whats the point? > Am I missing something here? > > regards, > George E