From: Florian Gross Date: 2005-01-26T06:10:53+09:00 Subject: Re: Injecting methods from one class into another. --------------090308080104080902090102 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit George Moschovitis wrote: > 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? If you're subclass provides an overwritten version of the base method this trick still allows you to call the base one: irb(main):012:0> Object.instance_method(:to_s).bind(2459).call => "#" irb(main):013:0> Object.instance_method(:to_s).bind(nil).call => "#" This can for example be used to implement a super() that skips over some of the inheritance chain members (see attachment.) or for sand boxes were you want to make sure that you are calling a secure method that is defined in Object and not an eventually overwritten version of it. --------------090308080104080902090102 Content-Type: application/x-ruby; name="superjump.rb" Content-Transfer-Encoding: base64 Content-Disposition: inline; filename="superjump.rb" IyBMaWtlIHN1cGVyIHRoaXMgY2FsbHMgbWV0aG9kcyBmcm9tIHRoZSBpbmhlcml0YW5jZSBj aGFpbiB3aGljaCB0aGlzDQojIG1ldGhvZCBpcyByZXBsYWNpbmcuIEhvd2V2ZXIgdGhpcyB2 ZXJzaW9uIGp1bXBzIG92ZXIgb25lIG9yIG1vcmUNCiMgbWV0aG9kcyBpbiB0aGUgaW5oZXJp dGFuY2UgY2hhaW4uIEl0IGlzIHVzZWQgbGlrZSB0aGlzOg0KIw0KIyAgIGNsYXNzIFgNCiMg ICAgIGRlZiBpdDsgcCAiWCNpdCI7IGVuZA0KIyAgIGVuZA0KIyAgIA0KIyAgIGNsYXNzIFkg PCBYDQojICAgICBkZWYgaXQ7IHAgIlkjaXQiOyBlbmQNCiMgICBlbmQNCiMgICANCiMgICBj bGFzcyBaIDwgWQ0KIyAgICAgZGVmIGl0DQojICAgICAgIHAgIlojaXQiDQojICAgICAgIHN1 cGVyanVtcA0KIyAgICAgZW5kDQojICAgZW5kDQojICAgDQojICAgWi5uZXcuaXQgIyBvdXRw dXRzICJYI2l0IiwgIlojaXQiDQojDQojIEJlIGNhcmVm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