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.5"
Class Attribute Summary collapse
-
.truth_test ⇒ Proc
Determines the truth test to apply (for CustomBoolean truthiness).
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?(expr) ⇒ Boolean
Tests whether expression 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.
155 156 157 158 |
# File 'lib/custom_boolean.rb', line 155 def initialize(truth_value, block_value) self.truth_value = truth_value self.value = block_value end |
Class Attribute Details
.truth_test ⇒ Proc
Determines the truth test to apply (for CustomBoolean truthiness)
Built in Truth tests include:
CustomBoolean::RUBY_TRUTH
CustomBoolean::PYTHON_TRUTH
CustomBoolean::PERL_TRUTH
CustomBoolean::C_TRUTH
CustomBoolean::STRICT_TRUTH
121 122 123 |
# File 'lib/custom_boolean.rb', line 121 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?(expr) ⇒ Boolean
Tests whether expression is truthy according to CustomBoolean truthiness. CustomBoolean truthiness is determined by the proc referenced by CustomBoolean.truth_test.
137 138 139 |
# File 'lib/custom_boolean.rb', line 137 def truthy?(expr) self.truth_test.call(expr) end |
Instance Method Details
#+@ ⇒ Object
Prefixing if_ with *+* turns if-statement into if-expression by invoking #value on CustomBoolean object.
175 176 177 |
# File 'lib/custom_boolean.rb', line 175 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.
233 234 235 236 237 238 239 240 241 242 |
# File 'lib/custom_boolean.rb', line 233 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
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/custom_boolean.rb', line 195 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 |