Class: CustomBoolean
- Inherits:
-
Object
- Object
- CustomBoolean
- Defined in:
- lib/custom_boolean.rb,
lib/custom_boolean.rb,
lib/custom_boolean/version.rb
Overview
Defined Under Namespace
Modules: Operators
Constant Summary collapse
- InvalidConditional =
Class.new(StandardError)
- RUBY_TRUTH =
proc { |expr| expr }
- PYTHON_TRUTH =
proc { |expr| expr && !(expr.respond_to?(:empty?) && expr.empty?) && expr != 0 }
- STRICT_TRUTH =
proc { |expr| raise "Expression must be strictly true or false." if expr != true && expr != false; expr }
- PERL_TRUTH =
proc { |expr| expr && expr != 0 && expr != "" && expr != "0" }
- C_TRUTH =
proc { |expr| expr && expr != 0 }
- VERSION =
"0.1.4"
Class Attribute Summary collapse
-
.truth_test ⇒ Proc
The proc that defines the truth test.
Instance Attribute Summary collapse
- #truth_value ⇒ Boolean
-
#value ⇒ Object
The value of the block that was executed in the if/else_if/else.
Class Method Summary collapse
-
.truthy?(condition) ⇒ Boolean
Tests whether condition is truthy according to CustomBoolean truthiness.
Instance Method Summary collapse
-
#+@ ⇒ Object
Prefixing
if_with *+* turns if-statement into if-expression by invoking #value on CustomBoolean object. -
#else { ... } ⇒ CustomBoolean
(also: #else?, #else!)
Equivalent of else for CustomBoolean truthiness.
-
#else_if(condition) { ... } ⇒ CustomBoolean
(also: #else_if?, #else_if!, #elsif, #elsif?, #elsif!)
Equivalent of elsif for CustomBoolean truthiness.
-
#initialize(truth_value, block_value) ⇒ CustomBoolean
constructor
A new instance of CustomBoolean.
Constructor Details
#initialize(truth_value, block_value) ⇒ CustomBoolean
Returns a new instance of CustomBoolean.
145 146 147 148 |
# File 'lib/custom_boolean.rb', line 145 def initialize(truth_value, block_value) self.truth_value = truth_value self.value = block_value end |
Class Attribute Details
.truth_test ⇒ Proc
Returns The proc that defines the truth test.
101 102 103 |
# File 'lib/custom_boolean.rb', line 101 def truth_test @truth_test end |
Instance Attribute Details
#truth_value ⇒ Boolean
92 93 94 |
# File 'lib/custom_boolean.rb', line 92 def truth_value @truth_value end |
#value ⇒ Object
Returns The value of the block that was executed in the if/else_if/else.
96 97 98 |
# File 'lib/custom_boolean.rb', line 96 def value @value end |
Class Method Details
.truthy?(condition) ⇒ Boolean
Tests whether condition is truthy according to CustomBoolean truthiness. CustomBoolean truthiness is determined by the proc referenced by CustomBoolean.truth_test.
Built in Truth tests include:
CustomBoolean::RUBY_TRUTH
CustomBoolean::PYTHON_TRUTH
CustomBoolean::PERL_TRUTH
CustomBoolean::C_TRUTH
CustomBoolean::STRICT_TRUTH
127 128 129 |
# File 'lib/custom_boolean.rb', line 127 def truthy?(condition) self.truth_test.call(condition) end |
Instance Method Details
#+@ ⇒ Object
Prefixing if_ with *+* turns if-statement into if-expression by invoking #value on CustomBoolean object.
165 166 167 |
# File 'lib/custom_boolean.rb', line 165 def +@ self.value end |
#else { ... } ⇒ CustomBoolean Also known as: else?, else!
Equivalent of else for CustomBoolean truthiness. Must be chained after an if_() or an else_if()
Differs from regular else as it uses CustomBoolean truthiness
No other conditionals may be chained after an else. In event a conditional is chained after an else an InvalidConditional exception will be raised.
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/custom_boolean.rb', line 223 def else(&block) raise InvalidConditional, "No further conditionals allowed after an else." if self.truth_value == :else_reached if self.truth_value CustomBoolean.new(:else_reached, self.value) else bvalue = block.call CustomBoolean.new(:else_reached, bvalue) end end |
#else_if(condition) { ... } ⇒ CustomBoolean Also known as: else_if?, else_if!, elsif, elsif?, elsif!
Equivalent of elsif for CustomBoolean truthiness. Must be chained after an if_() or another else_if()
Differs from regular elsif as it uses CustomBoolean truthiness
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/custom_boolean.rb', line 185 def else_if(condition, &block) raise InvalidConditional, "No further conditionals allowed after an else." if self.truth_value == :else_reached if self.truth_value CustomBoolean.new(true, self.value) else truth = !!CustomBoolean.truthy?(condition) bvalue = block.call if truth CustomBoolean.new(truth, bvalue) end end |