Class: Code::Object::Boolean

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #code_and_operator, #code_different, #code_equal_equal, #code_equal_equal_equal, #code_exclamation_point, #code_exclusive_range, #code_inclusive_range, #code_or_operator, #code_self, #code_to_string, #falsy?, #hash, maybe, #multi_fetch, repeat, #sig, |

Constructor Details

#initialize(raw) ⇒ Boolean

Returns a new instance of Boolean.



8
9
10
# File 'lib/code/object/boolean.rb', line 8

def initialize(raw)
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

Class Method Details

.nameObject



12
13
14
# File 'lib/code/object/boolean.rb', line 12

def self.name
  "Boolean"
end

Instance Method Details

#call(**args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/code/object/boolean.rb', line 16

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  value = arguments.first&.value

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

#code_bitwise_and(value) ⇒ Object



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

def code_bitwise_and(value)
  Boolean.new(raw & value.raw)
end

#code_bitwise_or(value) ⇒ Object



40
41
42
# File 'lib/code/object/boolean.rb', line 40

def code_bitwise_or(value)
  Boolean.new(raw | value.raw)
end

#code_bitwise_xor(value) ⇒ Object



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

def code_bitwise_xor(value)
  Boolean.new(raw ^ value.raw)
end

#inspectObject



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

def inspect
  to_s
end

#succObject



52
53
54
# File 'lib/code/object/boolean.rb', line 52

def succ
  Boolean.new(!raw)
end

#to_sObject



56
57
58
# File 'lib/code/object/boolean.rb', line 56

def to_s
  raw.to_s
end

#truthy?Boolean

Returns:



60
61
62
# File 'lib/code/object/boolean.rb', line 60

def truthy?
  raw
end