Class: TrueClass

Inherits:
Object show all
Defined in:
lib/errgonomic/core_ext/bool.rb,
lib/errgonomic/core_ext/blank.rb

Overview

Lift booleans into Option and Result, following Rust's bool: then_some, and ok_or/ok_or_else from nightly. Rust splits the lazy form into then, but that name is core Ruby (Kernel#then), so then_some takes either a value or a block. Rust's ok_or returns Result<(), E>; Ruby has no unit type, so Ok carries true.

Instance Method Summary collapse

Instance Method Details

#blank?false

true is not blank:

true.blank? # => false

Returns:

  • (false)


86
87
88
# File 'lib/errgonomic/core_ext/blank.rb', line 86

def blank?
  false
end

#ok_or(_err) ⇒ Object

Examples:

true.ok_or(:ohno) # => Ok(true)


29
30
31
# File 'lib/errgonomic/core_ext/bool.rb', line 29

def ok_or(_err)
  Ok(true)
end

#ok_or_elseObject

Examples:

true.ok_or_else { :ohno } # => Ok(true)


35
36
37
# File 'lib/errgonomic/core_ext/bool.rb', line 35

def ok_or_else
  Ok(true)
end

#present?Boolean

:nodoc:

Returns:

  • (Boolean)


90
91
92
# File 'lib/errgonomic/core_ext/blank.rb', line 90

def present? # :nodoc:
  true
end

#then_some(*args, &block) ⇒ Object

Examples:

true.then_some(:hello) # => Some(:hello)
true.then_some { :hello } # => Some(:hello)
true.then_some(nil) # => Some(nil)
true.then_some # => raise Errgonomic::ArgumentError, "then_some takes either a value or a block"


17
18
19
20
21
22
23
24
25
# File 'lib/errgonomic/core_ext/bool.rb', line 17

def then_some(*args, &block)
  if args.length == 1 && !block
    Some(args[0])
  elsif args.empty? && block
    Some(block.call)
  else
    raise Errgonomic::ArgumentError, 'then_some takes either a value or a block'
  end
end