Class: Array

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
mrblib/array.rb,
mrblib/array.rb,
src/array.c

Overview

15.2.12

Instance Method Summary (collapse)

Methods included from Enumerable

#__sort_sub__, #all?, #any?, #collect, #detect, #each_with_index, #entries, #find_all, #grep, #hash, #include?, #inject, #max, #min, #partition, #reject, #sort

Constructor Details

- (Array) initialize(size = 0, obj = nil, &block)

Private method for Array creation.

ISO 15.2.12.5.15

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'mrblib/array.rb', line 67

def initialize(size=0, obj=nil, &block)
  raise TypeError, "expected Integer for 1st argument" unless size.kind_of? Integer
  raise ArgumentError, "negative array size" if size < 0

  self.clear
  if size > 0
    self[size - 1] = nil # allocate

    idx = 0
    while idx < size
      self[idx] = (block)? block.call(idx): obj
      idx += 1
    end
  end

  self
end

Instance Method Details

- (Object) <=>(other)

Comparison—Returns an integer (-1, 0, or +1)

if this array is less than, equal to, or greater than <i>other_ary</i>.
Each object in each array is compared (using <=>). If any value isn't
equal, then that inequality is the return value. If all the
values found are equal, then the return is based on a
comparison of the array lengths.  Thus, two arrays are
"equal" according to <code>Array#<=></code> if and only if they have
the same length and the value of each element is equal to the
value of the corresponding element in the other array.

ISO 15.2.12.5.36 (x)



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'mrblib/array.rb', line 152

def <=>(other)
  other = self.__ary_cmp(other)
  return 0 if 0 == other
  return nil if nil == other

  len = self.size
  n = other.size
  len = n if len > n
  i = 0
  while i < len
    n = (self[i] <=> other[i])
    return n if n.nil? || n != 0
    i += 1
  end
  len = self.size - other.size
  if len == 0
    0
  elsif len > 0
    1
  else
    -1
  end
end

- (Object) ==(other)

Equality—Two arrays are equal if they contain the same number

of elements and if each element is equal to (according to
Object.==) the corresponding element in the other array.

ISO 15.2.12.5.33 (x)



109
110
111
112
113
114
115
116
117
118
119
120
# File 'mrblib/array.rb', line 109

def ==(other)
  other = self.__ary_eq(other)
  return false if other == false
  return true  if other == true
  len = self.size
  i = 0
  while i < len
    return false if self[i] != other[i]
    i += 1
  end
  return true
end

- (Object) __svalue

internal method to convert multi-value to single value



188
189
190
191
# File 'mrblib/array.rb', line 188

def __svalue
  return self.first if self.size < 2
  self
end

- (Object) _inspect



85
86
87
88
# File 'mrblib/array.rb', line 85

def _inspect
  return "[]" if self.size == 0
  "["+self.map{|x|x.inspect}.join(", ")+"]"
end

- (Object) collect!(&block) Also known as: map!

Calls the given block for each element of self and pass the respective element. Each element will be replaced by the resulting values.

ISO 15.2.12.5.7



50
51
52
53
54
55
# File 'mrblib/array.rb', line 50

def collect!(&block)
  return to_enum :collect! unless block_given?

  self.each_index { |idx| self[idx] = block.call(self[idx]) }
  self
end

- (Object) delete(key, &block)

Delete element with index key



178
179
180
181
182
183
184
185
# File 'mrblib/array.rb', line 178

def delete(key, &block)
  while i = self.index(key)
    self.delete_at(i)
    ret = key
  end
  return block.call if ret.nil? && block
  ret
end

- (Object) each(&block)

Calls the given block for each element of self and pass the respective element.

ISO 15.2.12.5.10



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'mrblib/array.rb', line 12

def each(&block)
  return to_enum :each unless block_given?

  idx, length = -1, self.length-1
  while idx < length and length <= self.length and length = self.length-1
    elm = self[idx += 1]
    unless elm
      if elm.nil? and length >= self.length
        break
      end
    end
    block.call(elm)
  end
  self
end

- (Object) each_index(&block)

Calls the given block for each element of self and pass the index of the respective element.

ISO 15.2.12.5.11



33
34
35
36
37
38
39
40
41
42
# File 'mrblib/array.rb', line 33

def each_index(&block)
  return to_enum :each_index unless block_given?

  idx = 0
  while idx < length
    block.call(idx)
    idx += 1
  end
  self
end

- (Boolean) eql?(other)

Returns true if self and other are the same object,

or are both arrays with the same content.

ISO 15.2.12.5.34 (x)

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
138
# File 'mrblib/array.rb', line 127

def eql?(other)
  other = self.__ary_eq(other)
  return false if other == false
  return true  if other == true
  len = self.size
  i = 0
  while i < len
    return false unless self[i].eql?(other[i])
    i += 1
  end
  return true
end

- (Object) inspect Also known as: to_s

Return the contents of this array as a string.

ISO 15.2.12.5.31 (x)



93
94
95
96
97
98
99
# File 'mrblib/array.rb', line 93

def inspect
  begin
    self._inspect
  rescue SystemStackError
    "[...]"
  end
end

- (Object) sort!(&block)

Sort all elements and replace self with these elements.



203
204
205
# File 'mrblib/array.rb', line 203

def sort!(&block)
  self.replace(self.sort(&block))
end