Forum: Ruby anonymous class, body defined by Proc.new vs. lambda

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2cb736dc6ab6b986406912b22dff9ea7?d=identicon&s=25 Thomas Hafner (Guest)
on 2009-04-25 23:20
(Received via mailing list)
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
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-04-25 23:35
(Received via mailing list)
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...
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-04-25 23:43
(Received via mailing list)
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>
This topic is locked and can not be replied to.