Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/mug/iff.rb,
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
-
#_? ⇒ FragileMethodChain
Begins a FragileMethodChain.
-
#and(default) {|obj| ... } ⇒ Object
Returns either
objordefault, depending on the falsiness ofobj. -
#and_then {|obj| ... } ⇒ Object
Calls
blockifobjis truthy. -
#iff?(*condition) { ... } ⇒ Boolean
Test for logical equivalence.
-
#iter_for(meth, *args) ⇒ Iterator
(also: #to_iter)
Creates an Iterator object, which is a subclass of Enumerator that recursively invokes a method on an object.
-
#maybe { ... } ⇒ Object, ...
Invokes a method on this object iff it is truthy, otherwise returns this object.
-
#or(default) {|obj| ... } ⇒ Object
Returns either
objordefault, depending on the truthiness ofobj. -
#or_then {|obj| ... } ⇒ Object
Calls
blockifobjis falsey. -
#revapply(*args) {|obj, *args| ... } ⇒ Object, Enumerator
(also: #cede)
Yields this object (and any other arguments) to a block.
-
#self {|o| ... } ⇒ Object
(also: #itself)
Returns this object.
-
#to_b ⇒ Boolean
Converts
objto a boolean using "typical" C-like conversion rules. -
#to_bool ⇒ Boolean
Returns the truthiness of
obj, as eithertrueorfalse. -
#yield(&block) ⇒ Object
Deprecated alias for #self.
Instance Method Details
#_? ⇒ FragileMethodChain
Begins a FragileMethodChain.
85 86 87 |
# File 'lib/mug/fragile-method-chain.rb', line 85 def _? FragileMethodChain.new(self) end |
#and(default) {|obj| ... } ⇒ 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.
19 20 21 22 23 24 25 |
# File 'lib/mug/and-or.rb', line 19 def and default, &_block if block_given? yield(self) ? default : self else self && default end end |
#and_then {|obj| ... } ⇒ Object
Calls block if obj is truthy.
Returns obj.
63 64 65 66 |
# File 'lib/mug/and-or.rb', line 63 def and_then &_block yield self if self self end |
#iff?(*condition) { ... } ⇒ Boolean
Test for logical equivalence.
Returns true if condition and obj are either
both truthy, or both falsey.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mug/iff.rb', line 22 def iff? *condition if condition.length == 1 cond = condition[0] elsif condition.empty? && block_given? cond = yield else raise ArgumentError, "wrong number of arguments (given #{condition.length}, expected 1)" end cond ? !!self : !self end |
#iter_for(meth, *args) ⇒ Iterator Also known as: to_iter
Creates an Iterator object, which is a subclass of Enumerator that recursively invokes a method on an object.
Initially the receiving object is self. After each iteration,
the receiving object is replaced with the result of the previous
iteration.
22 23 24 25 26 |
# File 'lib/mug/iterator/for.rb', line 22 def iter_for meth, *args Iterator.new(self) do |o| o.send(meth, *args) end end |
#maybe { ... } ⇒ Object, ...
Invokes a method on this object iff it is truthy, otherwise returns this object.
When a block is given, the block is invoked in the scope of
this object (i.e. self in the block refers to this object).
When no block is given, returns a MaybeDelegator that conditionally delegates methods to this object.
63 64 65 66 67 68 69 |
# File 'lib/mug/maybe.rb', line 63 def maybe &b if b self && instance_eval(&b) else MaybeDelegator.new(self) end end |
#or(default) {|obj| ... } ⇒ 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.
42 43 44 45 46 47 48 |
# File 'lib/mug/and-or.rb', line 42 def or default, &_block if block_given? yield(self) ? self : default else self || default end end |
#or_then {|obj| ... } ⇒ Object
Calls block if obj is falsey.
Returns obj.
81 82 83 84 |
# File 'lib/mug/and-or.rb', line 81 def or_then &_block yield self unless self self end |
#revapply(*args) {|obj, *args| ... } ⇒ Object, Enumerator Also known as: cede
Yields this object (and any other arguments) to a block. If no block is given, returns an Enumerator.
51 52 53 54 55 56 57 |
# File 'lib/mug/self.rb', line 51 def revapply(*args, &_block) if block_given? yield self, *args else enum_for(:revapply, *args) { args.length + 1 } end end |
#self {|o| ... } ⇒ Object Also known as: itself
Without a block, equivalent to Object#itself (Ruby 2.2+).
With a block, equivalent to Object#then (Ruby 2.6+).
This is different from #tap because obj.tap{nil} returns obj,
but obj.self{nil} returns nil.
Returns this object.
If a block is given, this object is yielded to it, and the result is returned.
24 25 26 27 28 29 30 |
# File 'lib/mug/self.rb', line 24 def self(&_block) if block_given? yield self else self end end |
#to_b ⇒ Boolean
Converts obj to a boolean using "typical" C-like conversion rules.
The following values all become false:
0,0.0, etc. (any numeric zero)Float::NAN""(empty string)[], {}, etc. (any Enumerable or Enumerator with no elements)- any Exception
All other values become true.
58 59 60 |
# File 'lib/mug/bool.rb', line 58 def to_b true end |
#to_bool ⇒ Boolean
Returns the truthiness of obj, as either true or false.
This is functionally equivalent to calling !!obj.
33 34 35 |
# File 'lib/mug/bool.rb', line 33 def to_bool true end |
#yield(&block) ⇒ Object
Deprecated alias for #self
34 35 36 37 |
# File 'lib/mug/self.rb', line 34 def yield(&block) #:nodoc: warn 'Object#yield is deprecated; use Object#self' self.self(&block) end |