Actually, I think the best way to do delegation would be to
have a #define_method that didn't change self. If you wanted
to delegate obj.bar to #foo (in the current class), it would be
nice to be able to do something like this:
# Proc/Method arg instead of block will leave self alone
define_method(:foo,obj.method(:bar))
Right now (only in v1.9 - where a block can be a block
argument), you'd do it like this:
In message "Re: A comparison by example of keyword argument > styles" > on Tue, 25 Oct 2005 00:15:35 +0900, Eric Mahurin > <eric_mahurin@yahoo.com> writes:
>> otherwise we
>> must change the code as
>>
>> def foo(*args, **keys)
>> bar(*args, **keys)
>> end
>>
>> everywhere, to just do delegation.
>
>or rather:
>
>def foo(*args, **keys, &block)
> bar(*args, **keys, &block)
>end
>
>What's wrong with having to do that?
It's longer than it is really needed. I want to delegate
everything
I've passed, that's all. I'd rather delegate blocks as well
when I
write bar(*args).
matz.
__________________________________
Yahoo! FareChase: Search multiple travel sites in one click. http://farechase.yahoo.com
def foo(*args, **keys, &block)
bar(*args, **keys, &block)
end
What's wrong with having to do that?
It's longer than it is really needed. I want to delegate
everything
I've passed, that's all. I'd rather delegate blocks as well
when I
write bar(*args).
matz.
Actually, I think the best way to do delegation would be to
have a #define_method that didn't change self. If you wanted
to delegate obj.bar to #foo (in the current class), it would be
nice to be able to do something like this:
# Proc/Method arg instead of block will leave self alone
define_method(:foo,obj.method(:bar))
Right now (only in v1.9 - where a block can be a block
argument), you'd do it like this:
In message "Re: A comparison by example of keyword argument >>styles" >> on Tue, 25 Oct 2005 00:15:35 +0900, Eric Mahurin >><eric_mahurin@yahoo.com> writes:
The "method" above is not meant to mean whatever method you
feel like. I'm talking about Object#method that returns a
Method object for the method name/symbol given as an argument.
I'm talking about being able to do something like this:
class C
obj = "hello world!"
define_method(:foo,obj.method(:))
end
C.new.foo(0,5) # => "hello"
Instead you get an error like this because define_method tries
to redefine self for obj.method(:).
TypeError: bind argument must be a subclass of String
from (irb):3:in `define_method'
from (irb):3
···
--- Trans <transfire@gmail.com> wrote:
> define_method(:foo,obj.method(:bar))
You'd be calling the method right then and there.
obj = "a"
def obj.method(k) ; "#{k} hey!" ; end
define_method(:foo,obj.method(:bar))
same as
define_method(:foo,"bar hey!")
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
The "method" above is not meant to mean whatever method you
feel like. I'm talking about Object#method that returns a
Method object for the method name/symbol given as an argument.
Ah, I see. I misinterpreted what you were asking for. Then try:
That's the ticket. I thought &method would be the same as
&(method.to_proc) - they both convert a Method to a Proc. But
I guess Method#to_proc does some isolation so that
define_method doesn't change self. We can also use this same
technique to give define_method a Proc where we want to leave
self alone:
me = lambda { self }
me.call # => main
C = Class.new{
# Proc#method(:call) is effectively Proc#to_method
define_method(:foo,me.method(:call).to_proc)
define_method(:bar,me)
}
C.new.foo # => main
C.new.bar # => #<C:0x40220d54>
Now if 1.8 just had Proc's that could take blocks, I'd be able
to get rid of some of my eval("def ...") statements and replace
them with define_method.
In 1.9, I think the above define_method/#to_proc way is the
best way to do simple delegation.
···
--- Trans <transfire@gmail.com> wrote:
Eric Mahurin wrote:
> The "method" above is not meant to mean whatever method you
> feel like. I'm talking about Object#method that returns a
> Method object for the method name/symbol given as an
argument.
Ah, I see. I misinterpreted what you were asking for. Then
try:
define_method(:foo,&obj.method(:bar).to_proc)
T.
__________________________________
Yahoo! FareChase: Search multiple travel sites in one click. http://farechase.yahoo.com
Sorry, I thought I tried this. I guess not. I falsely assumed
that define_method(:foo,&meth) and define_method(:foo,meth) did
the same thing (they do with Proc's). I guess not.
Here is a full suite to show what happens with
procs/methods/blocks/to_proc/method(:call) and define_method:
hello_world = "hello world!".method(:)
me = lambda { self }
define_method probably needs some documentation update to
describe this a little better.
···
--- nobu.nokada@softhome.net wrote:
Hi,
At Thu, 27 Oct 2005 01:07:49 +0900,
Eric Mahurin wrote in [ruby-talk:162757]:
> That's the ticket. I thought &method would be the same as
> &(method.to_proc) - they both convert a Method to a Proc.
But
Yes, they are equivalent.
__________________________________
Yahoo! FareChase: Search multiple travel sites in one click. http://farechase.yahoo.com
At Thu, 27 Oct 2005 01:58:09 +0900,
Eric Mahurin wrote in [ruby-talk:162763]:
> > That's the ticket. I thought &method would be the same as
> > &(method.to_proc) - they both convert a Method to a Proc.
> But
>
> Yes, they are equivalent.
Sorry, I thought I tried this. I guess not. I falsely assumed
that define_method(:foo,&meth) and define_method(:foo,meth) did
the same thing (they do with Proc's). I guess not.
I meant that &foo calls foo.to_proc internally.
Here is a full suite to show what happens with
procs/methods/blocks/to_proc/method(:call) and define_method:
hello_world = "hello world!".method(:)
me = lambda { self }