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.5"

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


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_testProc

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

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:

  • truth_test_proc (Proc)

    The Proc that defines the truth test

Returns:

  • (Proc)

    The proc that defines the truth test



121
122
123
# File 'lib/custom_boolean.rb', line 121

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?(expr) ⇒ Boolean

Tests whether expression is truthy according to CustomBoolean truthiness. CustomBoolean truthiness is determined by the proc referenced by CustomBoolean.truth_test.

Examples:

using C truthiness

CustomBoolean.truth_test = CustomBoolean::C_TRUTH
CustomBoolean.truthy?(0) #=> false

defining and using horse truthiness :)

# only :horse is *true*
CustomBoolean.truth_test = proc { |expr| expr == :horse }
CustomBoolean.truthy?(:horse) #=> true
CustomBoolean.truthy?(true) #=> false

Parameters:

  • expr

    an expression to evaluate

Returns:

  • (Boolean)


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.

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



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.

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:



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

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:



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