Created
February 6, 2018 15:28
-
-
Save sugamasao/6f5b3781c184bc59196f601a505ab073 to your computer and use it in GitHub Desktop.
each_causeみたいなのがほしい
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
class StandardError | |
def each_cause(&block) | |
e = self | |
while e | |
yield e | |
e = e.cause | |
end | |
end | |
end | |
begin | |
raise StandardError, '1st' | |
rescue => e1 | |
begin | |
raise StandardError, '2nd' | |
rescue => e2 | |
e2.each_cause do |ex| # <--- こんな感じ | |
puts ex.message | |
end | |
end | |
end | |
# % ruby each_cause_sample.rb | |
# 2nd | |
# 1st |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment