Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/mug/bool.rb,
lib/mug/self.rb,
lib/mug/maybe.rb,
lib/mug/and-or.rb,
lib/mug/iterator/for.rb,
lib/mug/fragile-method-chain.rb
Instance Method Summary collapse
-
#_? ⇒ Boolean
Begins a FragileMethodChain.
-
#and(default) ⇒ Object
Returns either
obj
ordefault
, depending on the falsiness ofobj
. -
#iter_for(meth, *args) ⇒ Object
(also: #to_iter)
Creates a new Iterator for the method named
meth
. -
#maybe(&b) ⇒ Object
Do something if this object is truthy.
-
#or(default) ⇒ Object
Returns either
obj
ordefault
, depending on the truthiness ofobj
. -
#self(&block) ⇒ Object
Returns this object.
-
#to_b ⇒ Object
Converts obj to a boolean.
-
#to_bool ⇒ Object
Converts obj to a boolean.
Instance Method Details
#_? ⇒ Boolean
Begins a FragileMethodChain.
50 51 52 |
# File 'lib/mug/fragile-method-chain.rb', line 50 def _? FragileMethodChain.new(self) end |
#and(default) ⇒ Object
Returns either obj
or default
, depending on the falsiness of obj
.
If a block is given, obj
is yielded to it; if it returns truthy, default
is returned, otherwise obj
is returned.
10 11 12 13 14 15 16 |
# File 'lib/mug/and-or.rb', line 10 def and default if block_given? yield(self) ? default : self else self && default end end |
#iter_for(meth, *args) ⇒ Object Also known as: to_iter
Creates a new Iterator for the method named meth
8 9 10 |
# File 'lib/mug/iterator/for.rb', line 8 def iter_for meth, *args Iterator.new self, meth, *args end |
#maybe(&b) ⇒ Object
Do something if this object is truthy.
If a block is given, it is executed in the context of this object, iff this object is neither nil
nor false
.
If no block is given, returns a MaybeDelegator object.
35 36 37 38 39 40 41 |
# File 'lib/mug/maybe.rb', line 35 def maybe &b if b self && instance_eval(&b) else MaybeDelegator.new(self) end end |
#or(default) ⇒ Object
Returns either obj
or default
, depending on the truthiness of obj
.
If a block is given, obj
is yielded to it; if it returns truthy, obj
is returned, otherwise default
is returned.
24 25 26 27 28 29 30 |
# File 'lib/mug/and-or.rb', line 24 def or default if block_given? yield(self) ? self : default else self || default end end |
#self(&block) ⇒ Object
Returns this object.
If a block is given, this object is yielded to it, and the result is returned.
9 10 11 12 13 14 15 |
# File 'lib/mug/self.rb', line 9 def self(&block) if block_given? yield self else self end end |