Created
January 8, 2014 08:17
-
-
Save windwiny/8313464 to your computer and use it in GitHub Desktop.
ruby proc, lambda difference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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