Created
December 29, 2011 00:42
-
-
Save donnoman/1530756 to your computer and use it in GitHub Desktop.
Ruby Raise in a block test
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
ree-1.8.7-2010.02 :001 > def me | |
ree-1.8.7-2010.02 :002?> yield | |
ree-1.8.7-2010.02 :003?> end | |
=> nil | |
ree-1.8.7-2010.02 :004 > | |
ree-1.8.7-2010.02 :005 > me do | |
ree-1.8.7-2010.02 :006 > begin | |
ree-1.8.7-2010.02 :007 > raise "goobers" | |
ree-1.8.7-2010.02 :008?> rescue | |
ree-1.8.7-2010.02 :009?> p "I caught the goober" | |
ree-1.8.7-2010.02 :010?> end | |
ree-1.8.7-2010.02 :011?> end | |
"I caught the goober" | |
=> nil | |
ree-1.8.7-2010.02 :012 > | |
ree-1.8.7-2010.02 :013 > | |
ree-1.8.7-2010.02 :014 > me do | |
ree-1.8.7-2010.02 :015 > raise "gobblers" rescue p "I cought the gobbler" | |
ree-1.8.7-2010.02 :016?> end | |
"I cought the gobbler" | |
=> nil | |
ree-1.8.7-2010.02 :017 > | |
ruby-1.9.2-p180 :001 > def me | |
ruby-1.9.2-p180 :002?> yield | |
ruby-1.9.2-p180 :003?> end | |
=> nil | |
ruby-1.9.2-p180 :004 > | |
ruby-1.9.2-p180 :005 > me do | |
ruby-1.9.2-p180 :006 > begin | |
ruby-1.9.2-p180 :007 > raise "goobers" | |
ruby-1.9.2-p180 :008?> rescue | |
ruby-1.9.2-p180 :009?> p "I caught the goober" | |
ruby-1.9.2-p180 :010?> end | |
ruby-1.9.2-p180 :011?> end | |
"I caught the goober" | |
=> "I caught the goober" | |
ruby-1.9.2-p180 :012 > | |
ruby-1.9.2-p180 :013 > | |
ruby-1.9.2-p180 :014 > me do | |
ruby-1.9.2-p180 :015 > raise "gobblers" rescue p "I cought the gobbler" | |
ruby-1.9.2-p180 :016?> end | |
"I cought the gobbler" | |
=> "I cought the gobbler" | |
ruby-1.9.2-p180 :017 > |
what you can't do is an implicit rescue in a block ie: omit the begin unless you are using the modifier form.
ruby-1.9.2-p180 :020 > me do
ruby-1.9.2-p180 :021 > raise "kerfluffle"
ruby-1.9.2-p180 :022?> rescue
ruby-1.9.2-p180 :023?> p "I cought the kerfluffle"
ruby-1.9.2-p180 :024?> end
SyntaxError: (irb):22: syntax error, unexpected keyword_rescue, expecting keyword_end
rescue
^
from /Users/donovan/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `
yup. that explains it.
see, did learn something today :)
Cheers man!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
weeeeeird.
ruby-1.9.2-p290 :001 > def me; yield; end
'=> nil
ruby-1.9.2-p290 :002 > me { raise "boo!"
ruby-1.9.2-p290 :003?> rescue => e
ruby-1.9.2-p290 :004?> puts e
ruby-1.9.2-p290 :005?> }
SyntaxError: (irb):3: syntax error, unexpected keyword_rescue, expecting '}'
rescue => e
^
from /Users/matt/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in
<main>' ruby-1.9.2-p290 :006 > me do raise "boo!" ruby-1.9.2-p290 :007?> rescue => e ruby-1.9.2-p290 :008?> puts e ruby-1.9.2-p290 :009?> end SyntaxError: (irb):7: syntax error, unexpected keyword_rescue, expecting keyword_end rescue => e ^ from /Users/matt/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in
ruby-1.9.2-p290 :010 > me do raise "boo!"
ruby-1.9.2-p290 :011?> rescue
ruby-1.9.2-p290 :012?> puts "boo!"
ruby-1.9.2-p290 :013?> end
SyntaxError: (irb):11: syntax error, unexpected keyword_rescue, expecting keyword_end
from /Users/matt/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `'
ruby-1.9.2-p290 :014 >