A comparison by example of keyword argument styles

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:

define_method(:foo) { |*args,&block|
  obj.bar(*args,&block)
}

or:

meth = obj.method(:bar)
define_method(:foo) { |*args,&block|
  meth.call(*args,&block)
}

···

--- Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

Hi,

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

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!")

T.

Eric Mahurin wrote:

Hi,

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.

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:

define_method(:foo) { |*args,&block|
  obj.bar(*args,&block)
}

or:

meth = obj.method(:bar)
define_method(:foo) { |*args,&block|
  meth.call(*args,&block)
}

__________________________________ Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com

Why not simply this:

   def foo(*args, &block)
     deletegate_to obj, :meth1, :meth2
     # same as
     obj.meth1.meth2(*args, &block)
   end

Cheers,
Daniel

···

--- Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

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

Daniel Schierbeck ha scritto:

Why not simply this:

  def foo(*args, &block)
    deletegate_to obj, :meth1, :meth2
    # same as
    obj.meth1.meth2(*args, &block)
  end

FWIW, I'd like to empower alias:

  alias foo x.foo

I know that alias is not a "text replacement" thing, but hey, I'd like this anyway :slight_smile:

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.

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

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.

···

--
Nobu Nakada

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 }

Test = Class.new {
define_method(:hi1,&hello_world)
define_method(:hi2,&(hello_world.to_proc))
define_method(:hi3,hello_world)
define_method(:hi4,hello_world.to_proc)
define_method(:me0) { self }
define_method(:me1,&me)
define_method(:me2,&(me.to_proc))
define_method(:me3,&(me.method(:call).to_proc))
define_method(:me4,me)
define_method(:me5,me.to_proc)
define_method(:me6,me.method(:call).to_proc)
}

t = Test.new
t.hi1(0,5) # => "hello"
t.hi2(0,5) # => "hello"
t.hi3(0,5) # TypeError: bind argument ...
t.hi4(0,5) # => "hello"
t.me0 # => #<Test:0x402e7edc> # self changed
t.me1 # => #<Test:0x402e7edc> # self changed
t.me2 # => #<Test:0x402e7edc> # self changed
t.me3 # => main
t.me4 # => #<Test:0x402e7edc> # self changed
t.me5 # => #<Test:0x402e7edc> # self changed
t.me6 # => main

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

Hi,

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 }

Test = Class.new {
define_method(:hi1,&hello_world)
define_method(:hi2,&(hello_world.to_proc))
define_method(:hi3,hello_world)
define_method(:hi4,hello_world.to_proc)
define_method(:me0) { self }
define_method(:me1,&me)
define_method(:me2,&(me.to_proc))
define_method(:me3,&(me.method(:call).to_proc))

It is same as:
   define_method(:me3) {me.call}

Object#method encloses its context, including "self".

···

--
Nobu Nakada