Created
March 23, 2011 21:24
-
-
Save george/884026 to your computer and use it in GitHub Desktop.
playing around with inline rescues
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
#!/usr/bin/env ruby -wKU | |
# describe "enigma" do | |
# it "fails, but why" do | |
# lambda { begin; 2 / 0 rescue NoMethodError; end }. | |
# should raise_error(ZeroDivisionError) | |
# end | |
# the inline rescue is swallowing the ZeroDivisionError exception and | |
# returning the NoMethodError class | |
# this is essentially the same as: | |
begin | |
2 / 0 rescue NoMethodError # => NoMethodError | |
end | |
# to see it more clearly | |
begin | |
2 / 0 rescue "NoMethodError" # => "NoMethodError" | |
end | |
# or | |
begin | |
2 / 0 rescue "I've been rescued!" # => "I've been rescued!" | |
end | |
# it "passes, but why" do | |
# lambda { begin; 2 / 0; rescue NoMethodError; end }. | |
# should raise_error(ZeroDivisionError) | |
# end | |
# end | |
# here you are no longer dealing with an inline rescue | |
begin | |
2 / 0 | |
rescue NoMethodError | |
"never sees the light of day" | |
end | |
# ~> -:37:in `/': divided by 0 (ZeroDivisionError) | |
# ~> from -:37 | |
# but... | |
begin | |
2 / 0 | |
rescue NoMethodError | |
"never sees the light of day" # => | |
rescue ZeroDivisionError | |
"division by zero is undefined" # => "division by zero is undefined" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment