1 | ##
|
---|
2 | # Array
|
---|
3 | #
|
---|
4 | # ISO 15.2.12
|
---|
5 | class Array
|
---|
6 |
|
---|
7 | ##
|
---|
8 | # Calls the given block for each element of +self+
|
---|
9 | # and pass the respective element.
|
---|
10 | #
|
---|
11 | # ISO 15.2.12.5.10
|
---|
12 | def each(&block)
|
---|
13 | return to_enum :each unless block_given?
|
---|
14 |
|
---|
15 | idx, length = -1, self.length-1
|
---|
16 | while idx < length and length <= self.length and length = self.length-1
|
---|
17 | elm = self[idx += 1]
|
---|
18 | unless elm
|
---|
19 | if elm.nil? and length >= self.length
|
---|
20 | break
|
---|
21 | end
|
---|
22 | end
|
---|
23 | block.call(elm)
|
---|
24 | end
|
---|
25 | self
|
---|
26 | end
|
---|
27 |
|
---|
28 | ##
|
---|
29 | # Calls the given block for each element of +self+
|
---|
30 | # and pass the index of the respective element.
|
---|
31 | #
|
---|
32 | # ISO 15.2.12.5.11
|
---|
33 | def each_index(&block)
|
---|
34 | return to_enum :each_index unless block_given?
|
---|
35 |
|
---|
36 | idx = 0
|
---|
37 | while idx < length
|
---|
38 | block.call(idx)
|
---|
39 | idx += 1
|
---|
40 | end
|
---|
41 | self
|
---|
42 | end
|
---|
43 |
|
---|
44 | ##
|
---|
45 | # Calls the given block for each element of +self+
|
---|
46 | # and pass the respective element. Each element will
|
---|
47 | # be replaced by the resulting values.
|
---|
48 | #
|
---|
49 | # ISO 15.2.12.5.7
|
---|
50 | def collect!(&block)
|
---|
51 | return to_enum :collect! unless block_given?
|
---|
52 |
|
---|
53 | self.each_index { |idx| self[idx] = block.call(self[idx]) }
|
---|
54 | self
|
---|
55 | end
|
---|
56 |
|
---|
57 | ##
|
---|
58 | # Alias for collect!
|
---|
59 | #
|
---|
60 | # ISO 15.2.12.5.20
|
---|
61 | alias map! collect!
|
---|
62 |
|
---|
63 | ##
|
---|
64 | # Private method for Array creation.
|
---|
65 | #
|
---|
66 | # ISO 15.2.12.5.15
|
---|
67 | def initialize(size=0, obj=nil, &block)
|
---|
68 | raise TypeError, "expected Integer for 1st argument" unless size.kind_of? Integer
|
---|
69 | raise ArgumentError, "negative array size" if size < 0
|
---|
70 |
|
---|
71 | self.clear
|
---|
72 | if size > 0
|
---|
73 | self[size - 1] = nil # allocate
|
---|
74 |
|
---|
75 | idx = 0
|
---|
76 | while idx < size
|
---|
77 | self[idx] = (block)? block.call(idx): obj
|
---|
78 | idx += 1
|
---|
79 | end
|
---|
80 | end
|
---|
81 |
|
---|
82 | self
|
---|
83 | end
|
---|
84 |
|
---|
85 | def _inspect
|
---|
86 | return "[]" if self.size == 0
|
---|
87 | "["+self.map{|x|x.inspect}.join(", ")+"]"
|
---|
88 | end
|
---|
89 | ##
|
---|
90 | # Return the contents of this array as a string.
|
---|
91 | #
|
---|
92 | # ISO 15.2.12.5.31 (x)
|
---|
93 | def inspect
|
---|
94 | begin
|
---|
95 | self._inspect
|
---|
96 | rescue SystemStackError
|
---|
97 | "[...]"
|
---|
98 | end
|
---|
99 | end
|
---|
100 | # ISO 15.2.12.5.32 (x)
|
---|
101 | alias to_s inspect
|
---|
102 |
|
---|
103 | ##
|
---|
104 | # Equality---Two arrays are equal if they contain the same number
|
---|
105 | # of elements and if each element is equal to (according to
|
---|
106 | # Object.==) the corresponding element in the other array.
|
---|
107 | #
|
---|
108 | # ISO 15.2.12.5.33 (x)
|
---|
109 | def ==(other)
|
---|
110 | other = self.__ary_eq(other)
|
---|
111 | return false if other == false
|
---|
112 | return true if other == true
|
---|
113 | len = self.size
|
---|
114 | i = 0
|
---|
115 | while i < len
|
---|
116 | return false if self[i] != other[i]
|
---|
117 | i += 1
|
---|
118 | end
|
---|
119 | return true
|
---|
120 | end
|
---|
121 |
|
---|
122 | ##
|
---|
123 | # Returns <code>true</code> if +self+ and _other_ are the same object,
|
---|
124 | # or are both arrays with the same content.
|
---|
125 | #
|
---|
126 | # ISO 15.2.12.5.34 (x)
|
---|
127 | def eql?(other)
|
---|
128 | other = self.__ary_eq(other)
|
---|
129 | return false if other == false
|
---|
130 | return true if other == true
|
---|
131 | len = self.size
|
---|
132 | i = 0
|
---|
133 | while i < len
|
---|
134 | return false unless self[i].eql?(other[i])
|
---|
135 | i += 1
|
---|
136 | end
|
---|
137 | return true
|
---|
138 | end
|
---|
139 |
|
---|
140 | ##
|
---|
141 | # Comparison---Returns an integer (-1, 0, or +1)
|
---|
142 | # if this array is less than, equal to, or greater than <i>other_ary</i>.
|
---|
143 | # Each object in each array is compared (using <=>). If any value isn't
|
---|
144 | # equal, then that inequality is the return value. If all the
|
---|
145 | # values found are equal, then the return is based on a
|
---|
146 | # comparison of the array lengths. Thus, two arrays are
|
---|
147 | # "equal" according to <code>Array#<=></code> if and only if they have
|
---|
148 | # the same length and the value of each element is equal to the
|
---|
149 | # value of the corresponding element in the other array.
|
---|
150 | #
|
---|
151 | # ISO 15.2.12.5.36 (x)
|
---|
152 | def <=>(other)
|
---|
153 | other = self.__ary_cmp(other)
|
---|
154 | return 0 if 0 == other
|
---|
155 | return nil if nil == other
|
---|
156 |
|
---|
157 | len = self.size
|
---|
158 | n = other.size
|
---|
159 | len = n if len > n
|
---|
160 | i = 0
|
---|
161 | while i < len
|
---|
162 | n = (self[i] <=> other[i])
|
---|
163 | return n if n.nil? || n != 0
|
---|
164 | i += 1
|
---|
165 | end
|
---|
166 | len = self.size - other.size
|
---|
167 | if len == 0
|
---|
168 | 0
|
---|
169 | elsif len > 0
|
---|
170 | 1
|
---|
171 | else
|
---|
172 | -1
|
---|
173 | end
|
---|
174 | end
|
---|
175 |
|
---|
176 | ##
|
---|
177 | # Delete element with index +key+
|
---|
178 | def delete(key, &block)
|
---|
179 | while i = self.index(key)
|
---|
180 | self.delete_at(i)
|
---|
181 | ret = key
|
---|
182 | end
|
---|
183 | return block.call if ret.nil? && block
|
---|
184 | ret
|
---|
185 | end
|
---|
186 |
|
---|
187 | # internal method to convert multi-value to single value
|
---|
188 | def __svalue
|
---|
189 | return self.first if self.size < 2
|
---|
190 | self
|
---|
191 | end
|
---|
192 | end
|
---|
193 |
|
---|
194 | ##
|
---|
195 | # Array is enumerable
|
---|
196 | class Array
|
---|
197 | # ISO 15.2.12.3
|
---|
198 | include Enumerable
|
---|
199 |
|
---|
200 | ##
|
---|
201 | # Sort all elements and replace +self+ with these
|
---|
202 | # elements.
|
---|
203 | def sort!(&block)
|
---|
204 | self.replace(self.sort(&block))
|
---|
205 | end
|
---|
206 | end
|
---|