Last active
December 29, 2015 13:49
-
-
Save Fitzsimmons/7680102 to your computer and use it in GitHub Desktop.
Showing the various ways of returning a value from a block (or failing to do so).
This file contains 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
irb(main):001:0> A = [1,2,3] | |
=> [1, 2, 3] | |
irb(main):002:0> def test_next | |
irb(main):003:1> puts A.map{|x| next x}.inspect | |
irb(main):004:1> puts "Finished nexting!" | |
irb(main):005:1> end | |
=> nil | |
irb(main):006:0> test_next | |
[1, 2, 3] | |
Finished nexting! | |
---- | |
irb(main):007:0> def test_break | |
irb(main):008:1> puts A.map{|x| break x}.inspect | |
irb(main):009:1> puts "Finished breaking!" | |
irb(main):010:1> end | |
=> nil | |
irb(main):011:0> test_break | |
1 | |
Finished breaking! | |
=> nil | |
---- | |
irb(main):012:0> def test_return | |
irb(main):013:1> puts A.map{|x| return x}.inspect | |
irb(main):014:1> puts "Finished returning!" | |
irb(main):015:1> end | |
=> nil | |
irb(main):016:0> test_return | |
=> 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment