Class: Boolean

Inherits:
Object show all
Defined in:
opal/opal/corelib/boolean.rb,
opal/opal/corelib/marshal/write_buffer.rb

Direct Known Subclasses

FalseClass, TrueClass

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

See: https://github.com/opal/opal/issues/2230

This is a hack that allows you to add methods to TrueClass and FalseClass. Do note, that while true and false have a correct $$class (it's either TrueClass or FalseClass), their prototype is Boolean.$$prototype, which basically means that when calling true.something we actually call Boolean#something instead of TrueClass#something. So using method_missing we dispatch it to TrueClass/FalseClass#something correctly.

The downside is that a correct implementation would also allow us to override the methods defined on Boolean, but our implementation doesn't allow that, unless you define them on Boolean and not on TrueClass/FalseClass.



97
98
99
100
101
# File 'opal/opal/corelib/boolean.rb', line 97

def method_missing(method, *args, &block)
  `var body = self.$$class.$$prototype['$' + #{method}]`
  super unless `typeof body !== 'undefined' && !body.$$stub`
  `Opal.send(self, body, #{args}, #{block})`
end

Class Method Details

.allocateObject

Raises:



31
32
33
# File 'opal/opal/corelib/boolean.rb', line 31

def allocate
  raise TypeError, "allocator undefined for #{name}"
end

Instance Method Details

#!Object



44
45
46
# File 'opal/opal/corelib/boolean.rb', line 44

def !
  `self != true`
end

#&(other) ⇒ Object



48
49
50
# File 'opal/opal/corelib/boolean.rb', line 48

def &(other)
  `(self == true) ? (other !== false && other !== nil) : false`
end

#==(other) ⇒ Object Also known as: equal?, eql?



60
61
62
# File 'opal/opal/corelib/boolean.rb', line 60

def ==(other)
  `(self == true) === other.valueOf()`
end

#^(other) ⇒ Object



56
57
58
# File 'opal/opal/corelib/boolean.rb', line 56

def ^(other)
  `(self == true) ? (other === false || other === nil) : (other !== false && other !== nil)`
end

#__id__Object Also known as: object_id



38
39
40
# File 'opal/opal/corelib/boolean.rb', line 38

def __id__
  `self.valueOf() ? 2 : 0`
end

#__marshal__(buffer) ⇒ Object



8
9
10
11
12
13
14
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 8

def __marshal__(buffer)
  if `self`
    buffer.append('T')
  else
    buffer.append('F')
  end
end

#clone(freeze: true) ⇒ Object



81
82
83
# File 'opal/opal/corelib/boolean.rb', line 81

def clone(freeze: true)
  self
end

#dupObject



77
78
79
# File 'opal/opal/corelib/boolean.rb', line 77

def dup
  self
end

#respond_to_missing?(method, _include_all = false) ⇒ Boolean

Returns:



103
104
105
106
# File 'opal/opal/corelib/boolean.rb', line 103

def respond_to_missing?(method, _include_all = false)
  `var body = self.$$class.$$prototype['$' + #{method}]`
  `typeof body !== 'undefined' && !body.$$stub`
end

#singleton_classObject



67
68
69
# File 'opal/opal/corelib/boolean.rb', line 67

def singleton_class
  `self.$$meta`
end

#to_sObject Also known as: inspect



71
72
73
# File 'opal/opal/corelib/boolean.rb', line 71

def to_s
  `(self == true) ? 'true' : 'false'`
end

#|(other) ⇒ Object



52
53
54
# File 'opal/opal/corelib/boolean.rb', line 52

def |(other)
  `(self == true) ? true : (other !== false && other !== nil)`
end