anonymous class, body defined by Proc.new vs. lambda

Discussion in 'Ruby' started by Thomas Hafner, Apr 25, 2009.

  1. Hello,

    this is OK for both, Ruby 1.8 and 1.9:
    f = Proc.new{}; Class.new(&f)

    But why is that valid for Ruby 1.8 only?
    f = lambda{}; Class.new(&f)

    Ruby 1.9 complains
    `initialize': wrong number of arguments (1 for 0) (ArgumentError)
    from ./now.rb:21:in `new'
    from ./now.rb:21:in `<main>'

    Regards
    Thomas
    Thomas Hafner, Apr 25, 2009
    #1
    1. Advertising

  2. Thomas Hafner

    Phlip Guest

    Thomas Hafner wrote:

    > Ruby 1.9 complains
    > `initialize': wrong number of arguments (1 for 0) (ArgumentError)
    > from ./now.rb:21:in `new'
    > from ./now.rb:21:in `<main>'


    Just a guess does this fix it?

    f = lambda{|o| }; Class.new(&f)

    Something to do with lambdas defend their arity...
    Phlip, Apr 25, 2009
    #2
    1. Advertising

  3. Thomas Hafner

    Ryan Davis Guest

    On Apr 25, 2009, at 14:20 , Thomas Hafner wrote:

    > this is OK for both, Ruby 1.8 and 1.9:
    > f = Proc.new{}; Class.new(&f)
    >
    > But why is that valid for Ruby 1.8 only?
    > f = lambda{}; Class.new(&f)
    >
    > Ruby 1.9 complains
    > `initialize': wrong number of arguments (1 for 0) (ArgumentError)
    > from ./now.rb:21:in `new'
    > from ./now.rb:21:in `<main>'


    the error message tells you why. it isn't hard to figure out what the
    arg is either:

    >> f = lambda{|x| p x }; Class.new(&f)

    #<Class:0x428ec>
    Ryan Davis, Apr 25, 2009
    #3
    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. David Lozzi
    Replies:
    3
    Views:
    1,928
    David Lozzi
    Jun 1, 2005
  2. Reporter
    Replies:
    3
    Views:
    470
    Mike Schilling
    May 12, 2007
  3. Minkoo Seo

    Proc vs lambda vs proc

    Minkoo Seo, Feb 4, 2007, in forum: Ruby
    Replies:
    19
    Views:
    241
    Brian Candler
    Feb 6, 2007
  4. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    173
    Sean O'Halpin
    Mar 30, 2009
  5. Stu
    Replies:
    31
    Views:
    374
    Steve Klabnik
    Apr 27, 2011
Loading...

Share This Page