Class: Array::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
opal/opal/corelib/array/inheritance.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Wrapper

Returns a new instance of Wrapper



35
36
37
# File 'opal/opal/corelib/array/inheritance.rb', line 35

def initialize(*args, &block)
  @literal = Array.new(*args, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'opal/opal/corelib/array/inheritance.rb', line 39

def method_missing(*args, &block)
  result = @literal.__send__(*args, &block)

  if `result === #@literal`
    self
  else
    result
  end
end

Class Method Details

.[](*objects) ⇒ Object



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

def self.[](*objects)
  allocate(objects)
end

.allocate(array = []) ⇒ Object



19
20
21
22
23
# File 'opal/opal/corelib/array/inheritance.rb', line 19

def self.allocate(array = [])
  obj = super()
  `obj.literal = array`
  obj
end

.new(*args, &block) ⇒ Object



25
26
27
28
29
# File 'opal/opal/corelib/array/inheritance.rb', line 25

def self.new(*args, &block)
  obj = allocate
  obj.initialize(*args, &block)
  obj
end

Instance Method Details

#*(other) ⇒ Object

wrapped results



78
79
80
81
82
83
84
85
86
87
88
89
# File 'opal/opal/corelib/array/inheritance.rb', line 78

def *(other)
  %x{
    var result = #{@literal * other};

    if (result.$$is_array) {
      return #{self.class.allocate(`result`)}
    }
    else {
      return result;
    }
  }
end

#==(other) ⇒ Object



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

def ==(other)
  @literal == other
end

#[](index, length = undefined) ⇒ Object Also known as: slice



91
92
93
94
95
96
97
98
99
100
101
102
# File 'opal/opal/corelib/array/inheritance.rb', line 91

def [](index, length = undefined)
  %x{
    var result = #{@literal.slice(index, length)};

    if (result.$$is_array && (index.$$is_range || length !== undefined)) {
      return #{self.class.allocate(`result`)}
    }
    else {
      return result;
    }
  }
end

#eql?(other) ⇒ Boolean

Returns:



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

def eql?(other)
  @literal.eql?(other)
end

#flatten(level = undefined) ⇒ Object



110
111
112
# File 'opal/opal/corelib/array/inheritance.rb', line 110

def flatten(level = undefined)
  self.class.allocate(@literal.flatten(level))
end

#initialize_copy(other) ⇒ Object



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

def initialize_copy(other)
  @literal = `other.literal`.clone
end

#inspectObject



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

def inspect
  @literal.inspect
end

#respond_to?(name) ⇒ Boolean

Returns:



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

def respond_to?(name, *)
  super || @literal.respond_to?(name)
end

#to_aObject



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

def to_a
  @literal
end

#to_aryObject



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

def to_ary
  self
end

#uniqObject



106
107
108
# File 'opal/opal/corelib/array/inheritance.rb', line 106

def uniq
  self.class.allocate(@literal.uniq)
end