Skip to content

Instantly share code, notes, and snippets.

@windwiny
Created January 8, 2014 08:17
Show Gist options
  • Select an option

  • Save windwiny/8313464 to your computer and use it in GitHub Desktop.

Select an option

Save windwiny/8313464 to your computer and use it in GitHub Desktop.
ruby proc, lambda difference
# proc, lambda difference
# Proc.new/proc --> not check parameter, return outside method
# lambda/-> --> check parameter, return just return lambda
p '------------------'
def test_1
p 'begin'
p1 = Proc.new do |x,y| p [x,y]; return end
p1.call
p 'end'
end
test_1
p '------------------'
def test_2
p 'begin'
p1 = proc do |x,y| p [x,y]; return end
p1.call
p 'end'
end
test_2
p '------------------'
def test_3
p 'begin'
p1 = lambda do |x,y| p [x,y]; return end
p1.call 1,2
p 'end'
end
test_3
p '------------------'
def test_4
p 'begin'
p1 = -> { return }
p1.call
p 'end'
end
test_4
__END__
"------------------"
"begin"
[nil, nil]
"------------------"
"begin"
[nil, nil]
"------------------"
"begin"
[1, 2]
"end"
"------------------"
"begin"
"end"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment