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
-
#_? ⇒ Boolean
Begins a FragileMethodChain.
-
#and(default, &_block) ⇒ Object
Returns either
obj
ordefault
, depending on the falsiness ofobj
. -
#and_then {|_self| ... } ⇒ Object
Calls
block
ifobj
is truthy. -
#iff?(*condition) ⇒ Boolean
Test for logical equivalence.
-
#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, &_block) ⇒ Object
Returns either
obj
ordefault
, depending on the truthiness ofobj
. -
#or_then {|_self| ... } ⇒ Object
Calls
block
isobj
is not falsey. -
#revapply(*args, &_block) ⇒ Object
(also: #cede)
Yields this object (and any other arguments) to a block.
-
#self(&_block) ⇒ Object
(also: #itself)
Returns this object.
-
#to_b ⇒ Object
Converts obj to a boolean.
-
#to_bool ⇒ Object
Converts obj to a boolean.
-
#yield(&block) ⇒ Object
Deprecated alias for #self.
Instance Method Details
#_? ⇒ Boolean
Begins a FragileMethodChain.
67 68 69 |
# File 'lib/mug/fragile-method-chain.rb', line 67 def _? FragileMethodChain.new(self) end |
#and(default, &_block) ⇒ 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, &_block if block_given? yield(self) ? default : self else self && default end end |
#and_then {|_self| ... } ⇒ Object
Calls block
if obj
is truthy.
Returns obj
.
37 38 39 40 |
# File 'lib/mug/and-or.rb', line 37 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.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mug/iff.rb', line 10 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) ⇒ Object Also known as: to_iter
Creates a new Iterator for the method named meth
8 9 10 11 12 |
# File 'lib/mug/iterator/for.rb', line 8 def iter_for meth, *args Iterator.new(self) do |o| o.send(meth, *args) end 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.
44 45 46 47 48 49 50 |
# File 'lib/mug/maybe.rb', line 44 def maybe &b if b self && instance_eval(&b) else MaybeDelegator.new(self) end end |
#or(default, &_block) ⇒ 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, &_block if block_given? yield(self) ? self : default else self || default end end |
#or_then {|_self| ... } ⇒ Object
Calls block
is obj
is not falsey.
Returns obj
.
47 48 49 50 |
# File 'lib/mug/and-or.rb', line 47 def or_then &_block yield self unless self self end |
#revapply(*args, &_block) ⇒ Object Also known as: cede
Yields this object (and any other arguments) to a block. If no block is given, returns an Enumerator.
29 30 31 32 33 34 35 |
# File 'lib/mug/self.rb', line 29 def revapply(*args, &_block) if block_given? yield self, *args else enum_for(:revapply, *args) { args.length + 1 } end end |
#self(&_block) ⇒ Object Also known as: itself
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 |
#to_b ⇒ Object
Converts obj to a boolean.
20 21 22 |
# File 'lib/mug/bool.rb', line 20 def to_b true end |
#to_bool ⇒ Object
Converts obj to a boolean.
13 14 15 |
# File 'lib/mug/bool.rb', line 13 def to_bool true end |
#yield(&block) ⇒ Object
Deprecated alias for #self
19 20 21 22 |
# File 'lib/mug/self.rb', line 19 def yield(&block) #:nodoc: warn 'Object#yield is deprecated; use Object#self' self.self(&block) end |