Ruby proc vs lambda in initialize()

Posted by Jimmy Chu on Stack Overflow See other posts from Stack Overflow or by Jimmy Chu
Published on 2012-06-21T03:11:54Z Indexed on 2012/06/21 3:16 UTC
Read the original article Hit count: 195

Filed under:
|
|

I found out this morning that proc.new works in a class initialize method, but not lambda. Concretely, I mean:

class TestClass

  attr_reader :proc, :lambda

  def initialize
    @proc = Proc.new {puts "Hello from Proc"}
    @lambda = lambda {puts "Hello from lambda"}
  end

end

c = TestClass.new
c.proc.call
c.lambda.call

In the above case, the result will be:

Hello from Proc
test.rb:14:in `<main>': undefined method `call' for nil:NilClass (NoMethodError)

Why is that?

Thanks!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about lambda