Class: Boolean

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

Overview

use_strict: true backtick_javascript: 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.



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

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



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

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

Instance Method Details

#!Object



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

def !
  `self != true`
end

#&(other) ⇒ Object



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

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

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



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

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

#^(other) ⇒ Object



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

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

#__id__Object Also known as: object_id



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

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

#__marshal__(buffer) ⇒ Object



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

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

#clone(freeze: true) ⇒ Object



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

def clone(freeze: true)
  self
end

#dupObject



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

def dup
  self
end

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

Returns:



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

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

#singleton_classObject



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

def singleton_class
  `self.$$meta`
end

#to_sObject Also known as: inspect



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

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

#|(other) ⇒ Object



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

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