Class: MaybeDelegator
Overview
Invokes methods on a wrapped object, if that object is truthy.
Instance Method Summary collapse
-
#initialize(o) ⇒ MaybeDelegator
constructor
Creates a new MaybeDelegator, wrapping
o. -
#maybe ⇒ MaybeDelegator
Returns this MaybeDelegator object.
-
#method_missing(*a, &b) ⇒ Object
Calls the method on
@oif it's truthy. -
#respond_to_missing?(meth, priv) ⇒ Boolean
This is a bit flakey, but I think it's meaningful.
Constructor Details
#initialize(o) ⇒ MaybeDelegator
Creates a new MaybeDelegator, wrapping o
12 13 14 |
# File 'lib/mug/maybe.rb', line 12 def initialize o @o = o end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*a, &b) ⇒ Object
Calls the method on @o if it's truthy.
26 27 28 |
# File 'lib/mug/maybe.rb', line 26 def method_missing *a, &b #:nodoc: @o && @o.send(*a, &b) end |
Instance Method Details
#maybe ⇒ MaybeDelegator
Returns this MaybeDelegator object.
21 22 23 |
# File 'lib/mug/maybe.rb', line 21 def maybe self end |
#respond_to_missing?(meth, priv) ⇒ Boolean
This is a bit flakey, but I think it's meaningful.
31 32 33 34 35 36 37 |
# File 'lib/mug/maybe.rb', line 31 def respond_to_missing? meth, priv if @o @o.repond_to_missing? meth, priv else true end end |