class Fibonaccia::Exception
Define a ‘parent’ exception class for the module. All module-specific exceptions should inherit from this.
Public Class Methods
We cannot access the mesg
‘instance variable’ of the inherited class hierarchy, do we needs must fake it as part of our constructor.
If the first element of the argument list is a string, we set our message to it. Otherwise, we follow the practice of using the name of the class as the message.
@param [Array] args
<tt>::StandardError.method(:new).arity => -1</tt>, so we allow an undefined number of arguments here, as well. We only look at the first one, though. If it's a string, we use it -- otherwise we set the message to <tt>nil</tt> and let #to_s/#to_str apply the default at need.
# File lib/fibonaccia/exceptions.rb, line 44 def initialize(*args) @mesg = (args[0].respond_to?(:to_str)) ? args[0] : nil return super end
Public Instance Methods
We cannot access the standard Exception
hierarchical message mechanism because it store the message in a non-@
prefixed ‘instance variable.’ So we need to work around it with our own instance variable, set by the constructor.
@return [String]
whatever the constructor stored in the <tt>@mesg</tt> instance variable, or the class name if <tt>@mesg</tt> is <tt>nil</tt>.
# File lib/fibonaccia/exceptions.rb, line 60 def to_s return (@mesg || self.class.name) end
Having a #to_str
method tells Ruby ‘you can treat me as a String.’ This just returns the same value as to_s
.
@return [String]
value returned by #to_s method.
# File lib/fibonaccia/exceptions.rb, line 71 def to_str return self.to_s end