define_method vs eval "def ..."

Discussion in 'Ruby' started by Tomasz Wegrzanowski, Aug 3, 2006.

  1. Hello,

    Just wondering, with eval "def ..." I can do:
    eval "def #{tag}(*args, &blk) xml:)#{tag}, *args, &blk) end"

    Of course it would be nicer to do:
    define_method(tag) {|*args,&blk|, xml(tag, *args, &blk) }
    But that throws a parse error.

    Is there some way to define method that takes a block argument
    without calling eval ?

    --
    Tomasz Wegrzanowski [ http://t-a-w.blogspot.com/ ]
    Tomasz Wegrzanowski, Aug 3, 2006
    #1
    1. Advertising

  2. On Aug 3, 2006, at 9:45 AM, Tomasz Wegrzanowski wrote:

    > Hello,
    >
    > Just wondering, with eval "def ..." I can do:
    > eval "def #{tag}(*args, &blk) xml:)#{tag}, *args, &blk) end"
    >
    > Of course it would be nicer to do:
    > define_method(tag) {|*args,&blk|, xml(tag, *args, &blk) }
    > But that throws a parse error.
    >
    > Is there some way to define method that takes a block argument
    > without calling eval ?
    >

    Ruby 1.9 lets blocks take blocks like define_method:)meth) { |*args,
    &blk| ... }

    Currently theres no way to do it in 1.8 AFAIK.

    > --
    > Tomasz Wegrzanowski [ http://t-a-w.blogspot.com/ ]
    >
    Logan Capaldo, Aug 3, 2006
    #2
    1. Advertising

  3. Tomasz Wegrzanowski

    Guest

    Hi,

    At Thu, 3 Aug 2006 22:45:40 +0900,
    Tomasz Wegrzanowski wrote in [ruby-talk:205997]:
    > Is there some way to define method that takes a block argument
    > without calling eval ?


    Using 1.9.

    --
    Nobu Nakada
    , Aug 3, 2006
    #3
  4. Tomasz Wegrzanowski

    Tammo Freese Guest

    Robert Dober wrote:
    [...]
    > and it seems to make it into Ruby2 since June
    > http://pub.cozmixng.org/~the-rwiki/?
    > cmd=view;name=Ruby2.0BlockParameterAndMultipleValueAssignment
    > which I consider extremly good news :)



    I am suprised that the blocks will work in such a way.
    How do these play together with the new define_method ?

    Examples from
    http://pub.cozmixng.org/~the-rwiki/?
    cmd=view;name=Ruby2.0BlockParameterAndMultipleValueAssignment
    :

    - Proc.new{|x| x}.call(0,1) returns 0.
    - Proc.new{|x,y| [x,y]}.call(0) returns [0,nil].

    So missing arguments seem to be set to nil, and additional arguments
    seem to be ignored. What would happen for this code fragment:

    proc_one = Proc.new{|x| x}
    proc_two = Proc.new{|x,y| [x,y]}

    define_method:)meth_one) &proc_one
    define_method:)meth_two) &proc_two

    p proc_one.call(0,1) # returns 0 according to the web page
    p proc_two.call(1) # returns 1,nil according to the web page

    p meth_one(0,1) # ???
    p meth_two(1) # ???

    Would the last two work and return the same result as the direct proc
    calls?
    If so, how would we define a method via define_method that takes a
    fixed number of parameters?
    If not, where does the magic happen that while the proc accepts the
    arguments,
    the method defined by the proc does not?



    Thanks for your help,

    Tammo
    Tammo Freese, Aug 4, 2006
    #4
  5. On 8/3/06, Tammo Freese <> wrote:
    >
    > Robert Dober wrote:
    > [...]
    > > and it seems to make it into Ruby2 since June
    > > http://pub.cozmixng.org/~the-rwiki/?
    > > cmd=view;name=Ruby2.0BlockParameterAndMultipleValueAssignment
    > > which I consider extremly good news :)

    >
    >
    > I am suprised that the blocks will work in such a way.
    > How do these play together with the new define_method ?
    >
    > Examples from
    > http://pub.cozmixng.org/~the-rwiki/?
    > cmd=view;name=Ruby2.0BlockParameterAndMultipleValueAssignment
    > :
    >
    > - Proc.new{|x| x}.call(0,1) returns 0.
    > - Proc.new{|x,y| [x,y]}.call(0) returns [0,nil].
    >
    > So missing arguments seem to be set to nil, and additional arguments
    > seem to be ignored. What would happen for this code fragment:


    That first example is a change from 1.8

    Proc.new {|x| x}.call(0,1)
    (irb):58: warning: multiple values for a block parameter (2 for 1)
    from (irb):58
    => [0, 1]

    I'm not sure that I like this change.


    --
    Rick DeNatale

    IPMS/USA Region 12 Coordinator
    http://ipmsr12.denhaven2.com/

    Visit the Project Mercury Wiki Site
    http://www.mercuryspacecraft.com/
    Rick DeNatale, Aug 4, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiong Feng
    Replies:
    0
    Views:
    818
    Jiong Feng
    Nov 19, 2003
  2. Sean Ross
    Replies:
    3
    Views:
    121
    Aredridel
    Dec 25, 2003
  3. Replies:
    7
    Views:
    157
  4. planetthoughtful

    Newbie: def must come before call to def?

    planetthoughtful, Mar 12, 2007, in forum: Ruby
    Replies:
    4
    Views:
    126
    Pit Capitain
    Mar 12, 2007
  5. Kyung won Cheon
    Replies:
    0
    Views:
    203
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page