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

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.

Examples:

5.assert(Integer) # Passes
"string".assert(error: ArgumentError) { |s| s.size > 5 } # Passes
"".assert(message: "String too short") {|s| s.size > 5 } # Raises error with message

Yields:

  • (self)

    Optional block whose truthiness is asserted.



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)) || (message ? raise(error, message) : raise(error)) }
end

#if {|self| ... } ⇒ Object?

If the object is truthy (neither nil nor false), yields the object itself to the given block.

Examples:

123.if { |x| puts "Number is #{x}" } # Output: Number is 123
nil.if { |x| puts "This won't print: #{x}" } # Nothing
false.if { |x| puts "This also won't print: #{x}" } # Nothing
"hello".if { |s| s.upcase } # => "HELLO"

Yields:

  • (self)

    Gives self to the block if self is truthy.



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.

Examples:

42.iff(Integer) { |x| x * 2 } # => 84
"foo".iff(Float) { |n| n / 2 }.iff(String) { |s| s.upcase } # => "FOO"

Yields:

  • If ‘x === self` is true.



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.

Examples:

nil.or { |x| puts "Object was nil" } # Output: Object was nil
false.or { |x| puts "Object was false" } # Output: Object was false
123.or { |x| puts "This won't print: #{x}" } # Output: (nothing)
"".or { |s| s + "default" } # => "" (empty string is truthy)

Yields:

  • (self)

    Gives self to the block if self is falsy.



51
52
53
# File 'lib/patch/foobar.rb', line 51

def or
  self || yield(self)
end

#showObject

Prints the inspect representation of self to standard output. Returns self, allowing for chaining.

Examples:

[1, 2, 3]
   .show # prints [1, 2, 3]
   .map { |x| x * 2 }
   .show # print [2, 4, 6]


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.

Examples:

value = "test".tif { |s| puts "Processing #{s}" } # Output: Processing test
puts value # Output: test

data = [1, 2, 3]
data.tif { |arr| arr.pop if arr.size > 2 }
puts data.inspect # Output: [1, 2] (if data was not empty)

Yields:

  • (self)

    Gives self to the block if self is truthy.



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.

Examples:

result = nil.tor { |x| puts "Handled nil case" } # Output: Handled nil case
puts result.inspect # Output: nil

user_input = "".empty? && nil # user_input is nil
user_input.tor { puts "This will print" }

Yields:

  • (self)

    Gives self to the block if self is falsy.

See Also:



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.

Examples:

status_code = 200
status_code
  .when(200..299) { puts "Yay!" } # Only one to run
  .when(400..499) { puts "Client Error!" }
  .when(500..599) { puts "Server Error!" }

# Only `Array`'s block runs here
[1,2,3,4,5,6]
    .when(Array) { puts "It's an array" }
    .when(String) { puts "It's a string" }

# Chaining `default` (alias for `tor`)
"hello"
  .when(Integer) { |n| puts "#{n} + 1 = #{n + 1}" }
  .default { puts "This won't run either" }

# If value was nil:
# nil.when(Integer){}.default { puts "this will print" }

Yields:

  • If ‘x === self` is true.



101
102
103
# File 'lib/patch/foobar.rb', line 101

def when(x = self)
  tap { yield(self) if x === self }
end