Class: Code::Object::Boolean

Inherits:
Code::Object show all
Defined in:
lib/code/object/boolean.rb

Constant Summary

Constants inherited from Code::Object

NUMBER_CLASSES

Instance Attribute Summary

Attributes inherited from Code::Object

#raw

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, as_json, #as_json, call, #code_and_operator, code_and_operator, #code_as_json, code_as_json, code_different, #code_different, #code_equal_equal, code_equal_equal, code_equal_equal_equal, #code_equal_equal_equal, code_exclamation_point, #code_exclamation_point, code_exclusive_range, #code_exclusive_range, code_inclusive_range, #code_inclusive_range, code_new, code_or_operator, #code_or_operator, code_self, #code_self, #code_to_json, code_to_json, falsy?, #falsy?, #hash, inspect, maybe, multi_fetch, #multi_fetch, #nothing?, nothing?, repeat, sig, #sig, #succ, #to_code, to_json, #to_json, to_s, truthy?, |

Constructor Details

#initialize(*args, **_kargs) ⇒ Boolean

Returns a new instance of Boolean.



6
7
8
# File 'lib/code/object/boolean.rb', line 6

def initialize(*args, **_kargs, &)
  @raw = (args.first.is_an?(Object) ? args.first.truthy? : !!args.first)
end

Instance Method Details

#call(**args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/code/object/boolean.rb', line 10

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "&", "bitwise_and"
    sig(args) { Boolean }
    code_bitwise_and(code_value)
  when "^", "bitwise_xor"
    sig(args) { Boolean }
    code_bitwise_xor(code_value)
  when "|", "bitwise_or"
    sig(args) { Boolean }
    code_bitwise_or(code_value)
  else
    super
  end
end

#code_bitwise_and(other) ⇒ Object



30
31
32
33
34
# File 'lib/code/object/boolean.rb', line 30

def code_bitwise_and(other)
  code_other = other.to_code

  Boolean.new(raw & code_other.raw)
end

#code_bitwise_or(other) ⇒ Object



36
37
38
39
40
# File 'lib/code/object/boolean.rb', line 36

def code_bitwise_or(other)
  code_other = other.to_code

  Boolean.new(raw | code_other.raw)
end

#code_bitwise_xor(other) ⇒ Object



42
43
44
45
46
# File 'lib/code/object/boolean.rb', line 42

def code_bitwise_xor(other)
  code_other = other.to_code

  Boolean.new(raw ^ code_other.raw)
end

#truthy?Boolean

Returns:



48
49
50
# File 'lib/code/object/boolean.rb', line 48

def truthy?
  raw
end