Class: CustomBoolean

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_boolean.rb,
lib/custom_boolean.rb,
lib/custom_boolean/version.rb

Overview

Author:

  • John Mair (banisterfiend)

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(truth_value, block_value) ⇒ CustomBoolean

Returns a new instance of CustomBoolean.

Parameters:

  • truth_value (Boolean, Symbol)
  • block_value


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_testProc

Returns The proc that defines the truth test.

Returns:

  • (Proc)

    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_valueBoolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/custom_boolean.rb', line 92

def truth_value
  @truth_value
end

#valueObject

Returns The value of the block that was executed in the if/else_if/else.

Returns:

  • (Object)

    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

Examples:

changing truthiness to a preset

CustomBoolean.truth_test = CustomBoolean::PYTHON_TRUTH

user-defined truthiness

# only :horse is true:
CustomBoolean.truth_test = proc { |expr| expr == :horse }

Parameters:

  • condition

    an expression to evaluate

Returns:

  • (Boolean)


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.

Examples:

single if-expression example

+if(true) { :hello } #=> :hello
+if(false) { :hello } #=> nil

if-else-expression example

+if(false) {
  :hello
}.
else {
  :goodbye
}
#=> :goodbye

Returns:

  • (Object)

    extracts the value of the if, transforming it into an if-expression



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.

Examples:

else example

if_(cond) {
  :hello
}.
else {
  :goodbye
}

Yields:

  • the block will be executed if the condition evalues to true (so long as no prior else_if or if has evaluated to true further up the chain)

Returns:

Raises:



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

Examples:

else_if example

if_(cond) {
  :hello
}.
else_if(cond2) {
  :goodbye
}

Parameters:

  • condition

    an expression to evaluate

Yields:

  • the block will be executed if the condition evalues to true (so long as no prior else_if or if has evaluated to true further up the chain)

Returns:

Raises:



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