Class: Boolean

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

Overview

use_strict: true

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.



92
93
94
95
96
# File 'opal/opal/corelib/boolean.rb', line 92

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

Class Method Details

.allocateObject



33
34
35
# File 'opal/opal/corelib/boolean.rb', line 33

def allocate
  ::Kernel.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: eql?, equal?



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



40
41
42
# File 'opal/opal/corelib/boolean.rb', line 40

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 == true`
    buffer.append('T')
  else
    buffer.append('F')
  end
end

#clone(freeze: true) ⇒ Object



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

def clone(freeze: true)
  self
end

#dupObject



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

def dup
  self
end

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

Returns:



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

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

#singleton_classObject



64
65
66
# File 'opal/opal/corelib/boolean.rb', line 64

def singleton_class
  `self.$$meta`
end

#to_sObject Also known as: inspect



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

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