Created
September 25, 2014 19:27
-
-
Save natesymer/7d763438828f62f902a8 to your computer and use it in GitHub Desktop.
Adds default messages to Exception subclasses.
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
require "./with_message.rb" | |
MyError = StandardError.with_message "This is my error message" | |
raise MyError # the raised error's message is "This is my error message" | |
raise MyError, "Something else" # the raised error's message is "Something else" |
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
#!/usr/bin/env ruby | |
class Exception | |
alias_method :initialize_original, :initialize | |
class << self | |
attr_accessor :default_message | |
def with_message msg | |
@global_message ||= nil | |
e = Class.new self | |
e.default_message = msg | |
e | |
end | |
end | |
def initialize msg=nil | |
initialize_original msg || self.class.default_message | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment