# Proc.new v. lambda

Is there a way to programmatically determine if an object was generated
by Proc.new versus lambda?

Really, you could have checked it yourself.

irb(main):001:0> a = Proc.new{}
=> #Proc:[email protected]:1(irb)
irb(main):002:0> b = lambda{}
=> #<Proc:[email protected](irb):2 (lambda)>
irb(main):003:0> a.lambda?
=> false
irb(main):004:0> b.lambda?
=> true

– Matma R.

Bartosz,

Wednesday, March 7, 2012, 8:25:57 AM, you wrote:

BD> Really, you could have checked it yourself.

irb(main):001:0>> a = Proc.new{}
=>> #Proc:[email protected]:1(irb)
irb(main):002:0>> b = lambda{}
=>> #<Proc:[email protected](irb):2 (lambda)>
irb(main):003:0>> a.lambda?
=>> false
irb(main):004:0>> b.lambda?
=>> true

BD> – Matma R.

First you have to know there is a lambda? function.

What I did do was:
irb(main):002:0> p = Proc.new {42}
=> #Proc:[email protected]:2(irb)
irb(main):003:0> l = lambda {43}
=> #<Proc:[email protected](irb):3 (lambda)>
irb(main):004:0> defined? p
=> “local-variable”
irb(main):006:0> defined? l
=> “local-variable”

irb(main):007:0> p.class
=> Proc
irb(main):008:0> l.class
=> Proc

So, given there is a lambda? function, why not a proc? function?

In terms of design, why isn’t lambda a super or sub class of Proc?

On Mar 7, 2012, at 11:32 AM, Ralph S. wrote:

In terms of design, why isn’t lambda a super or sub class of Proc?

It is always hard to answer ‘why?’ with respect to design decisions.

I don’t have any special insight but Ruby tends towards shallow
inheritance graphs and ‘wide’ interfaces. Instead of Collection, Array,
Stack, Queue, Dequeue, FixedArray, VariableArray, etc., there is just
Array with 88 methods (ruby 1.9.3-p125).

Similarly instead of Proc and Lambda, there is just Proc.

I guess the most accurate, but perhaps unsatisfying answer, regarding
the design of core Ruby classes is that they are that way because that
is what Matz likes.

Gary W.

2012/3/7 Ralph S. [email protected]:

First you have to know there is a lambda? function.

It’s all documented here: http://www.ruby-doc.org/core-1.9.3/Proc.html
Ruby’s docs are IMO pretty good. Also, in IRB you can always call
#methods or #instance_methods to see what the object can do, for ex.

irb(main):007:0> Proc.instance_methods(false).sort # “false” means
“don’t show inherited methods”
=> [:==, :===, :[], :arity, :binding, :call, :clone, :curry, :dup,
:eql?, :hash, :lambda?, :parameters, :source_location, :to_proc,
:to_s, :yield]

So, given there is a lambda? function, why not a proc? function?
In terms of design, why isn’t lambda a super or sub class of Proc?

These two I can’t answer. I guess it could be both ways.

– Matma R.