Skip to content
Tags

,

Difference between proc and lambda

January 5, 2012

Procs fall into two categories.

1. Procs that are lambda? – lambda procs

defined using lambda or ->()

ruby-1.9.2-p290 :086 > a = lambda{ puts "hello"}
=> # ruby-1.9.2-p290 :087 > a.class
=> Proc

2. Procs that aren’t – simple procs.
defined using Proc.new or proc

ruby-1.9.2-p290 :088 > a = Proc.new{ puts "hello"}
=> # ruby-1.9.2-p290 :089 > a.class
=> Proc

Differences between lambda and procs: (these may not be comprehensive)

1. Lambda’s are strict argument checkers, like methods, they can throw an ArgumentError exception. Simple procs will just ignore incorrect, extra or fewer argument combinations

ruby-1.9.2-p290 :013 > a = lambda { puts "Hello" }
=> #<Proc:0x000001009b1288@(irb):13 (lambda)>
ruby-1.9.2-p290 :014 > a.call
Hello
=> nil
ruby-1.9.2-p290 :015 > a.call 4
ArgumentError: wrong number of arguments (1 for 0)
from (irb):13:in `block in irb_binding'
from (irb):15:in `call'
from (irb):15
from /Users/gagan/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

ruby-1.9.2-p290 :016 > a = Proc.new { puts "Hello" }
=> #<Proc:0x00000100980f70@(irb):16>
ruby-1.9.2-p290 :017 > a.call
Hello
=> nil
ruby-1.9.2-p290 :018 > a.call 4
Hello
=> nil

2. Lambda’s act like methods regarding their return status – they can return values just like methods. When you try to return a value from a simple proc you end up with a  LocalJumpError

ruby-1.9.2-p290 :039 > a = lambda do
ruby-1.9.2-p290 :040 > puts "will return 5"
ruby-1.9.2-p290 :041?> 5
ruby-1.9.2-p290 :042?> end
=> #<Proc:0x0000010285beb8@(irb):39 (lambda)>
ruby-1.9.2-p290 :043 > a.call
will return 5
=> 5
ruby-1.9.2-p290 :044 > a = Proc.new do
ruby-1.9.2-p290 :045 > puts "will return 5"
ruby-1.9.2-p290 :046?> 5
ruby-1.9.2-p290 :047?> end
=> #<Proc:0x00000102846018@(irb):44>
ruby-1.9.2-p290 :048 > a.call
will return 5
=> 5
ruby-1.9.2-p290 :049 > a = lambda do
ruby-1.9.2-p290 :050 > puts "will return 5"
ruby-1.9.2-p290 :051?> return 5
ruby-1.9.2-p290 :052?> end
=> #<Proc:0x00000102835d30@(irb):49 (lambda)>
ruby-1.9.2-p290 :053 > a.call
will return 5
=> 5
ruby-1.9.2-p290 :054 > a = Proc.new do
ruby-1.9.2-p290 :055 > puts "will not return 5"
ruby-1.9.2-p290 :056?> return 5
ruby-1.9.2-p290 :057?> end
=> #<Proc:0x0000010216f5a0@(irb):54>
ruby-1.9.2-p290 :058 > a.call
will not return 5
LocalJumpError: unexpected return
from (irb):56:in `block in irb_binding'
from (irb):58:in `call'
from (irb):58
from /Users/gagan/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

ref: http://ablogaboutcode.com/2012/01/04/the-ampersand-operator-in-ruby/

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: