Skip to content

Instantly share code, notes, and snippets.

@george
Created March 23, 2011 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save george/884026 to your computer and use it in GitHub Desktop.
Save george/884026 to your computer and use it in GitHub Desktop.
playing around with inline rescues
#!/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