Skip to content

Instantly share code, notes, and snippets.

@kachick
Created November 4, 2015 12:03
Show Gist options
  • Save kachick/2398bd977de582d4d2b3 to your computer and use it in GitHub Desktop.
Save kachick/2398bd977de582d4d2b3 to your computer and use it in GitHub Desktop.
例外のrescueにmoduleを使う

Ruby の beginrescueend 構文では例外クラスを指定しろという説明が多い気するけど、includeしているmoduleとかでも拾えるんでもっと柔軟だよねというアレ

module Role end

class Foo < Exception
  include Role
end

begin
  raise Foo
rescue Role => err
  puts "We can rescued an error `#{err.inspect}` with an included module"
end

# => We can rescued an error `#<Foo: Foo>` with an included module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment