Proc and lambdas

Hi!

I cannot find the error - what is wrong here?

class Temp
     def xx(x, &pp)
     pp.call(x)
     end
end

t=Temp.new
p t.xx (3, {|y| print y," " })

thanks,
Andrew

Hello, Andrew.
I checked the code and I've found some mistakes:
When using lambdas, don't forget to use the keyword lambda:
t.xx(3, lambda {|x| print x, " "})
Or you can use Proc.new:

t.xx(3, Proc.new {|x| print x, " "})

If we run the code with this correction, we are going to have an ArgumentError. We specified a &pp and then we use 'pp'. Remove the '&'.
This code works:
class Temp
    def xx(x, pp)
        pp.call(x)
    end
end

t = Temp.new

p t.xx(3, lambda {|x| print x, " "})
Greetings,Ary Emmanuel.From: m.fellinger@gmail.com

···

Date: Wed, 30 Dec 2015 02:02:33 +0000
Subject: Re: proc and lambdas
To: ruby-talk@ruby-lang.org

p t.xx(3){|y| print y," " }

On Wed, Dec 30, 2015 at 2:43 AM Die Optimisten <inform@die-optimisten.net> wrote:
Hi!

I cannot find the error - what is wrong here?

class Temp

     def xx(x, &pp)

     pp.call(x)

     end

end

t=Temp.new

p t.xx (3, {|y| print y," " })

thanks,

Andrew

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>

<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe:

p t.xx(3){|y| print y," " }

···

On Wed, Dec 30, 2015 at 2:43 AM Die Optimisten <inform@die-optimisten.net> wrote:

Hi!

I cannot find the error - what is wrong here?

class Temp
     def xx(x, &pp)
     pp.call(x)
     end
end

t=Temp.new
p t.xx (3, {|y| print y," " })

thanks,
Andrew

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;