class Object
Public Instance Methods
Converts arg to a boolean (true or false).
# File lib/mug/bool.rb, line 5 def Bool(arg) !!arg end
Begins a FragileMethodChain
.
# File lib/mug/fragile-method-chain.rb, line 67 def _? FragileMethodChain.new(self) end
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.
# File lib/mug/and-or.rb, line 10 def and default, &_block if block_given? yield(self) ? default : self else self && default end end
Calls block
if obj
is truthy.
Returns obj
.
# File lib/mug/and-or.rb, line 37 def and_then &_block yield self if self self end
Test for logical equivalence.
Returns true if condition
and obj
are either both truthy, or both falsey.
# 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
Creates a new Iterator
for the method named meth
# File lib/mug/iterator/for.rb, line 8 def iter_for meth, *args Iterator.new(self) do |o| o.send(meth, *args) end end
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.
# File lib/mug/maybe.rb, line 44 def maybe &b if b self && instance_eval(&b) else MaybeDelegator.new(self) end end
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.
# File lib/mug/and-or.rb, line 24 def or default, &_block if block_given? yield(self) ? self : default else self || default end end
Calls block
is obj
is not falsey.
Returns obj
.
# File lib/mug/and-or.rb, line 47 def or_then &_block yield self unless self self end
Yields this object (and any other arguments) to a block. If no block is given, returns an Enumerator
.
# 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
Returns this object.
If a block is given, this object is yielded to it, and the result is returned.
# File lib/mug/self.rb, line 9 def self(&_block) if block_given? yield self else self end end
Converts obj to a boolean.
# File lib/mug/bool.rb, line 20 def to_b true end
Converts obj to a boolean.
# File lib/mug/bool.rb, line 13 def to_bool true end