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