Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/patch/foobar.rb
Overview
Extends the base Object class with utility methods for control flow, introspection, and data manipulation.
Instance Method Summary collapse
-
#assert(n = self, error: StandardError, message: nil) {|self| ... } ⇒ self
Asserts a condition about
self. -
#if {|self| ... } ⇒ Object?
If the object is truthy (neither
nilnorfalse), yields the object itself to the given block. -
#iff(x = self) { ... } ⇒ Object | self
Just like [Object#when], but propagates the values of the blocks that execute.
-
#or {|self| ... } ⇒ Object?
If the object is falsy (
nilorfalse), yields the object itself to the given block. -
#show ⇒ Object
Prints the
inspectrepresentation ofselfto standard output. -
#tif {|self| ... } ⇒ self
If the object is truthy, yields the object to the block and then returns
self. -
#tor {|self| ... } ⇒ self
If the object is falsy, yields the object to the block and then returns
self. -
#when(x = self) { ... } ⇒ self
If
xis case-equal (‘===`) toself, yields to the block and returnsself.
Instance Method Details
#assert(n = self, error: StandardError, message: nil) {|self| ... } ⇒ self
Asserts a condition about self. If a block is given, it asserts that the block returns a truthy value. The block is passed self as an argument. If no block is given, it asserts that ‘n === self` is true. If the assertion fails, it performs a non-local exit by raise.
140 141 142 |
# File 'lib/patch/foobar.rb', line 140 def assert(n = self, error: StandardError, message: nil) tap { (block_given? ? yield(self) : (n === self)) || ( ? raise(error, ) : raise(error)) } end |
#if {|self| ... } ⇒ Object?
If the object is truthy (neither nil nor false), yields the object itself to the given block.
17 18 19 |
# File 'lib/patch/foobar.rb', line 17 def if self && yield(self) end |
#iff(x = self) { ... } ⇒ Object | self
Just like [Object#when], but propagates the values of the blocks that execute. This method can be chained because it returns self if no block is executed.
115 116 117 118 119 120 121 |
# File 'lib/patch/foobar.rb', line 115 def iff(x = self) if x === self yield(self) else self end end |
#or {|self| ... } ⇒ Object?
If the object is falsy (nil or false), yields the object itself to the given block.
51 52 53 |
# File 'lib/patch/foobar.rb', line 51 def or self || yield(self) end |
#show ⇒ Object
Prints the inspect representation of self to standard output. Returns self, allowing for chaining.
152 153 154 |
# File 'lib/patch/foobar.rb', line 152 def show tap { puts inspect } end |
#tif {|self| ... } ⇒ self
If the object is truthy, yields the object to the block and then returns self. This is similar to #if, but always returns self, making it useful for chaining or conditional side effects.
35 36 37 |
# File 'lib/patch/foobar.rb', line 35 def tif tap { self && yield(self) } end |
#tor {|self| ... } ⇒ self
If the object is falsy, yields the object to the block and then returns self. This is similar to #or, but always returns the original object.
69 70 71 |
# File 'lib/patch/foobar.rb', line 69 def tor tap { self || yield(self) } end |
#when(x = self) { ... } ⇒ self
If x is case-equal (‘===`) to self, yields to the block and returns self. This is useful for chaining checks, mimicking a case-like structure.
101 102 103 |
# File 'lib/patch/foobar.rb', line 101 def when(x = self) tap { yield(self) if x === self } end |