Exception: Fibonaccia::Exception
- Inherits:
-
StandardError
- Object
- StandardError
- Fibonaccia::Exception
- Defined in:
- lib/fibonaccia/exceptions.rb
Overview
Define a ‘parent’ exception class for the module. All module-specific exceptions should inherit from this.
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(*args) ⇒ Exception
constructor
We cannot access the
mesg
‘instance variable’ of the inherited class hierarchy, do we needs must fake it as part of our constructor. -
#to_s ⇒ String
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. -
#to_str ⇒ String
Having a
#to_str
method tells Ruby ‘you can treat me as a String.’ This just returns the same value as #to_s.
Constructor Details
#initialize(*args) ⇒ Exception
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.
45 46 47 48 |
# File 'lib/fibonaccia/exceptions.rb', line 45 def initialize(*args) @mesg = (args[0].respond_to?(:to_str)) ? args[0] : nil return super end |
Instance Method Details
#to_s ⇒ String
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.
61 62 63 |
# File 'lib/fibonaccia/exceptions.rb', line 61 def to_s return (@mesg || self.class.name) end |
#to_str ⇒ String
Having a #to_str
method tells Ruby ‘you can treat me as a String.’ This just returns the same value as #to_s.
72 73 74 |
# File 'lib/fibonaccia/exceptions.rb', line 72 def to_str return self.to_s end |