[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Patch to delegate.rb

christophe.poucet

6/29/2005 2:55:00 PM

Hello,

I propose the following patch to delegate.rb. In fact it could be used
to replace the entire eval loop that adds methods to Delegator. It is
necessary because the delegator that is existent now will not find
methods that have been added after a delegator has been created.

foo = Object.new
def foo.foo
puts "foo"
end
foo.plop # => foo
foo2 = SimpleDelegator.new(foo)
foo2.plop # => foo
def foo.bar
puts "bar"
end
foo.bar # => bar
foo2.bar # => NoMethodError

The proposed patch will make the last line, "foo2.bar" react as
expected, namely... print "bar". The patch is:
-------------------------------------------------------------------------------
class SimpleDelegator
def method_missing(m, *args)
unless @_sd_obj.respond_to?(m)
super(m, *args)
end
@_sd_obj.__send__(m, *args)
end
end
-------------------------------------------------------------------------------
With regards,
Christophe

3 Answers

Yukihiro Matsumoto

6/30/2005 12:55:00 AM

0

Hi,

In message "Re: Patch to delegate.rb"
on Wed, 29 Jun 2005 23:55:41 +0900, christophe.poucet@gmail.com writes:

|I propose the following patch to delegate.rb. In fact it could be used
|to replace the entire eval loop that adds methods to Delegator. It is
|necessary because the delegator that is existent now will not find
|methods that have been added after a delegator has been created.

Nice idea. It will be merged. Thank you.

matz.


Austin Ziegler

6/30/2005 12:32:00 PM

0

On 6/29/05, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:
> In message "Re: Patch to delegate.rb"
> on Wed, 29 Jun 2005 23:55:41 +0900, christophe.poucet@gmail.com writes:
>
> |I propose the following patch to delegate.rb. In fact it could be used
> |to replace the entire eval loop that adds methods to Delegator. It is
> |necessary because the delegator that is existent now will not find
> |methods that have been added after a delegator has been created.
>
> Nice idea. It will be merged. Thank you.

Should not the same be done to #respond_to? on the delegator, or is
that not done normally?

-austin
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Yukihiro Matsumoto

6/30/2005 3:25:00 PM

0

Hi,

In message "Re: Patch to delegate.rb"
on Thu, 30 Jun 2005 21:31:37 +0900, Austin Ziegler <halostatue@gmail.com> writes:

|Should not the same be done to #respond_to? on the delegator, or is
|that not done normally?

You're right. Thank you.

matz.