Class: Array::Wrapper

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Wrapper

Returns a new instance of Wrapper



1527
1528
1529
# File 'opal/opal/corelib/array.rb', line 1527

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



1531
1532
1533
1534
1535
1536
1537
1538
1539
# File 'opal/opal/corelib/array.rb', line 1531

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

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

Class Method Details

.[](*objects) ⇒ Object



1523
1524
1525
# File 'opal/opal/corelib/array.rb', line 1523

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

.allocate(array = []) ⇒ Object



1511
1512
1513
1514
1515
# File 'opal/opal/corelib/array.rb', line 1511

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

.new(*args, &block) ⇒ Object



1517
1518
1519
1520
1521
# File 'opal/opal/corelib/array.rb', line 1517

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

Instance Method Details

#*(other) ⇒ Object

wrapped results



1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
# File 'opal/opal/corelib/array.rb', line 1570

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

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

#==(other) ⇒ Object



1549
1550
1551
# File 'opal/opal/corelib/array.rb', line 1549

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

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



1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
# File 'opal/opal/corelib/array.rb', line 1583

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

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

#eql?(other) ⇒ Boolean

Returns:



1553
1554
1555
# File 'opal/opal/corelib/array.rb', line 1553

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

#flatten(level = undefined) ⇒ Object



1602
1603
1604
# File 'opal/opal/corelib/array.rb', line 1602

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

#initialize_copy(other) ⇒ Object



1541
1542
1543
# File 'opal/opal/corelib/array.rb', line 1541

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

#inspectObject



1565
1566
1567
# File 'opal/opal/corelib/array.rb', line 1565

def inspect
  @literal.inspect
end

#respond_to?(name) ⇒ Boolean

Returns:



1545
1546
1547
# File 'opal/opal/corelib/array.rb', line 1545

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

#to_aObject



1557
1558
1559
# File 'opal/opal/corelib/array.rb', line 1557

def to_a
  @literal
end

#to_aryObject



1561
1562
1563
# File 'opal/opal/corelib/array.rb', line 1561

def to_ary
  self
end

#uniqObject



1598
1599
1600
# File 'opal/opal/corelib/array.rb', line 1598

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