4
J'ai découvert ce matin que proc.new fonctionne dans une méthode de initialisation de classe, mais pas lambda. Concrètement, je veux dire:Ruby proc vs lambda dans initialize()
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
Dans le cas ci-dessus, le résultat sera:
Hello from Proc
test.rb:14:in `<main>': undefined method `call' for nil:NilClass (NoMethodError)
Pourquoi?
Merci!
L'appel de 'Kernel.lambda' est une autre option. – Casper