Class: Hoodoo::StringInquirer
- Inherits:
-
String
- Object
- String
- Hoodoo::StringInquirer
- Defined in:
- lib/hoodoo/utilities/string_inquirer.rb
Overview
Given a string, provides an object that takes the string’s value and turns it into a method “#value?”, returning true
; other methods all respond false
.
Example:
greeting = Hoodoo::StringInquirer.new( 'hello' )
greeting.hello? # => true
greeting.hi? # => false
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments) ⇒ Object (private)
Called when a String receives a message it cannot handle. This is where StringInquirer adds in its string-value-dependent “fake” boolean method. For any method name ending in “?”, returns true
if the string value matches the name except for that “?”, else false
. If the method name does not end in “?”, the call is passed to super
.
method_name
-
Method name that wasn’t found in object instance.
- *arguments
-
List of arguments passed to the method.
46 47 48 49 50 51 52 |
# File 'lib/hoodoo/utilities/string_inquirer.rb', line 46 def method_missing( method_name, *arguments ) if method_name[ -1 ] == '?' self == method_name[ 0..-2 ] else super end end |