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

Instance Method Details

#_?FragileMethodChain

Begins a FragileMethodChain.

Examples:

require 'mug/fragile-method-chain'
# Similar to: a.b && a.b.c
# except that a.b is not called twice
a._?.b.c._!

# Also works with #[] method
nested_hash._?[:a][:b][:c]._!

Returns:



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.

Examples:

require 'mug/and-or'

get_a_list.and(default_list, &:empty?).do_something

Parameters:

  • default (Object)

    the value to return if the condition is met

Yields:

  • (obj)

    optional block to determine the condition

Returns:

  • (Object)

    either self or default



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.

Examples:

require 'mug/and-or'

try_thing.and_then {|result| log "got #{result.inspect}" }

Yields:

  • (obj)

    called if self is truthy

Returns:



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.

Examples:

require 'mug/iff'
"hello".iff?(true)   #=> true
"hello".iff?(false)  #=> false
nil.iff?(false)      #=> true
nil.iff?(true)       #=> false

Parameters:

  • condition (Object)

    a value to test against self

Yields:

  • when no positional argument is given, yields to obtain the condition

Returns:

  • (Boolean)

    true if both self and condition are truthy or both are falsey; false otherwise



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.

Examples:

require 'mug/iterator/for'
0.iter_for(:next).take(5)  #=> [1, 2, 3, 4, 5]
0.iter_for(:+, 2).take(5)  #=> [2, 4, 6, 8, 10]

Parameters:

  • meth (Symbol)

    the method name to invoke on each iteration

  • args (Array)

    additional arguments passed to the method

Returns:

  • (Iterator)

    a new Iterator instance



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.

Examples:

require 'mug/maybe'
# Equivalent to: a && a.b && a.b.c
# (block form)
a.maybe{ b.maybe{ c } }
# (delegator form)
a.maybe.b.maybe.c

Yields:

  • the block is evaluated in the scope of this object

Returns:

  • (Object, nil, false)

    the result of the block, or this object if falsy

  • (MaybeDelegator)

    if no block is given



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.

Examples:

require 'mug/and-or'

data_store.get_env_hash.or(default_hash).do_something

Parameters:

  • default (Object)

    the value to return if the condition is met

Yields:

  • (obj)

    optional block to determine the condition

Returns:

  • (Object)

    either self or default



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.

Examples:

require 'mug/and-or'

try_thing.or_then { log "failed" }

Yields:

  • (obj)

    called if self is falsey

Returns:



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.

Parameters:

  • args (Array)

    additional arguments to yield alongside self

Yields:

  • (obj, *args)

    yields self and any additional arguments

Yield Parameters:

  • obj (Object)

    this object

  • args (Object)

    additional arguments

Returns:



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

Note:

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.

Examples:

require 'mug/self'
1.self                          #=> 1
2.self{|i| i*3 }               #=> 6
[1,1,2,2,3].group_by(&:self)   #=> {1=>[1,1], 2=>[2,2], 3=>[3]}

Yields:

  • (o)

    optionally yields self to the block

Yield Parameters:

Returns:

  • (Object)

    self, or the result of the block



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_bBoolean

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.

Examples:

require 'mug/bool'
"hello".to_b  #=> true
"".to_b       #=> false
42.to_b       #=> true
0.to_b        #=> false

Returns:

  • (Boolean)

    C-like truthiness of self



58
59
60
# File 'lib/mug/bool.rb', line 58

def to_b
  true
end

#to_boolBoolean

Returns the truthiness of obj, as either true or false. This is functionally equivalent to calling !!obj.

Examples:

require 'mug/bool'
"hello".to_bool  #=> true
nil.to_bool      #=> false

Returns:

  • (Boolean)

    the truthiness of self



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