Class: Range
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- opal/opal/corelib/range.rb,
opal/opal/corelib/marshal/write_buffer.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Enumerable
#all?, #any?, #chunk, #chunk_while, #collect, #collect_concat, #compact, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #enumerator_size, #filter_map, #find_all, #find_index, #grep, #grep_v, #group_by, #inject, #lazy, #max_by, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_h, #to_set, #uniq, #zip
Constructor Details
#initialize(first, last, exclude = false) ⇒ Range
Returns a new instance of Range.
12
13
14
15
16
17
18
19
|
# File 'opal/opal/corelib/range.rb', line 12
def initialize(first, last, exclude = false)
::Kernel.raise ::NameError, "'initialize' called twice" if @begin
::Kernel.raise ::ArgumentError, 'bad value for range' unless first <=> last || first.nil? || last.nil?
@begin = first
@end = last
@excl = exclude
end
|
Instance Attribute Details
Returns the value of attribute begin.
10
11
12
|
# File 'opal/opal/corelib/range.rb', line 10
def begin
@begin
end
|
Returns the value of attribute end.
10
11
12
|
# File 'opal/opal/corelib/range.rb', line 10
def end
@end
end
|
Instance Method Details
281
282
283
284
285
286
287
|
# File 'opal/opal/corelib/range.rb', line 281
def %(n)
if @begin.is_a?(Numeric) && @end.is_a?(Numeric)
::Enumerator::ArithmeticSequence.new(self, n, :%)
else
step(n)
end
end
|
#===(value) ⇒ Object
21
22
23
|
# File 'opal/opal/corelib/range.rb', line 21
def ===(value)
include? value
end
|
#__marshal__(buffer) ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 152
def __marshal__(buffer)
buffer.save_link(self)
buffer.write_extends(self)
buffer.append('o')
buffer.append_symbol(self.class.name)
buffer.write_fixnum(3)
buffer.append_symbol('excl')
buffer.write(exclude_end?)
buffer.append_symbol('begin')
buffer.write(self.begin)
buffer.append_symbol('end')
buffer.write(self.end)
end
|
#bsearch(&block) ⇒ Object
289
290
291
292
293
294
295
296
297
298
299
300
301
|
# File 'opal/opal/corelib/range.rb', line 289
def bsearch(&block)
return enum_for(:bsearch) unless block_given?
if `is_infinite(self) && (self.begin.$$is_number || self.end.$$is_number)`
::Kernel.raise ::NotImplementedError, "Can't #bsearch an infinite range"
end
unless `self.begin.$$is_number && self.end.$$is_number`
::Kernel.raise ::TypeError, "can't do binary search for #{@begin.class}"
end
to_a.bsearch(&block)
end
|
#count(&block) ⇒ Object
34
35
36
37
38
39
|
# File 'opal/opal/corelib/range.rb', line 34
def count(&block)
if !block_given? && `is_infinite(self)`
return ::Float::INFINITY
end
super
end
|
#cover?(value) ⇒ Boolean
Also known as:
include?, member?
46
47
48
49
50
51
52
53
54
|
# File 'opal/opal/corelib/range.rb', line 46
def cover?(value)
beg_cmp = (@begin.nil? && -1) || (@begin <=> value) || false
end_cmp = (@end.nil? && -1) || (value <=> @end) || false
if @excl
end_cmp && end_cmp < 0
else
end_cmp && end_cmp <= 0
end && beg_cmp && beg_cmp <= 0
end
|
#each {|current| ... } ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'opal/opal/corelib/range.rb', line 56
def each(&block)
return enum_for(:each) { size } unless block_given?
%x{
var i, limit;
if (#{@begin}.$$is_number && #{@end}.$$is_number) {
if (#{@begin} % 1 !== 0 || #{@end} % 1 !== 0) {
#{::Kernel.raise ::TypeError, "can't iterate from Float"}
}
for (i = #{@begin}, limit = #{@end} + #{@excl ? 0 : 1}; i < limit; i++) {
block(i);
}
return self;
}
if (#{@begin}.$$is_string && #{@end}.$$is_string) {
#{@begin.upto(@end, @excl, &block)}
return self;
}
}
current = @begin
last = @end
unless current.respond_to?(:succ)
::Kernel.raise ::TypeError, "can't iterate from #{current.class}"
end
while @end.nil? || (current <=> last) < 0
yield current
current = current.succ
end
yield current if !@excl && current == last
self
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
98
99
100
101
102
103
104
|
# File 'opal/opal/corelib/range.rb', line 98
def eql?(other)
return false unless ::Range === other
@excl === other.exclude_end? &&
@begin.eql?(other.begin) &&
@end.eql?(other.end)
end
|
#exclude_end? ⇒ Boolean
106
107
108
|
# File 'opal/opal/corelib/range.rb', line 106
def exclude_end?
@excl
end
|
#first(n = undefined) ⇒ Object
110
111
112
113
114
|
# File 'opal/opal/corelib/range.rb', line 110
def first(n = undefined)
::Kernel.raise ::RangeError, 'cannot get the minimum of beginless range' if @begin.nil?
return @begin if `n == null`
super
end
|
317
318
319
|
# File 'opal/opal/corelib/range.rb', line 317
def hash
[::Range, @begin, @end, @excl].hash
end
|
307
308
309
|
# File 'opal/opal/corelib/range.rb', line 307
def inspect
"#{@begin && @begin.inspect}#{@excl ? '...' : '..'}#{@end && @end.inspect}"
end
|
#last(n = undefined) ⇒ Object
116
117
118
119
120
|
# File 'opal/opal/corelib/range.rb', line 116
def last(n = undefined)
::Kernel.raise ::RangeError, 'cannot get the maximum of endless range' if @end.nil?
return @end if `n == null`
to_a.last(n)
end
|
#marshal_load(args) ⇒ Object
311
312
313
314
315
|
# File 'opal/opal/corelib/range.rb', line 311
def marshal_load(args)
@begin = args[:begin]
@end = args[:end]
@excl = args[:excl]
end
|
FIXME: currently hardcoded to assume range holds numerics
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'opal/opal/corelib/range.rb', line 123
def max
if @end.nil?
::Kernel.raise ::RangeError, 'cannot get the maximum of endless range'
elsif block_given?
super
elsif !@begin.nil? && (@begin > @end ||
@excl && @begin == @end)
nil
else
`#{@excl} ? #{@end} - 1 : #{@end}`
end
end
|
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'opal/opal/corelib/range.rb', line 136
def min
if @begin.nil?
::Kernel.raise ::RangeError, 'cannot get the minimum of beginless range'
elsif block_given?
super
elsif !@end.nil? && (@begin > @end ||
@excl && @begin == @end)
nil
else
@begin
end
end
|
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'opal/opal/corelib/range.rb', line 149
def size
%x{
var b = this.begin, e = this.end;
// If begin is Numeric
if (#{::Numeric === `b`}) {
// If end is Numeric
if (#{::Numeric === `e`}) {
// Calculating size based on whether range is exclusive or inclusive
var size = #{`e` - `b`};
if (size < 0) {
return 0;
}
if (!this.excl) {
size += 1;
}
return (#{::Float === `b`} || #{::Float === `e`}) ? Math.floor(size) : size;
}
// If end is nil
else if (e === nil) {
return Infinity;
}
}
// If begin is nil
else if (b === nil) {
// If end is Numeric
if (#{::Numeric === `e`}) {
return Infinity;
}
}
// If neither begin nor end is Numeric
return nil;
}
end
|
#step(n = undefined) ⇒ Object
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'opal/opal/corelib/range.rb', line 185
def step(n = undefined)
%x{
function coerceStepSize() {
if (n == null) {
n = 1;
}
else if (!n.$$is_number) {
n = #{::Opal.coerce_to!(n, ::Integer, :to_int)}
}
if (n < 0) {
#{::Kernel.raise ::ArgumentError, "step can't be negative"}
} else if (n === 0) {
#{::Kernel.raise ::ArgumentError, "step can't be 0"}
}
}
function enumeratorSize() {
if (!#{@begin.respond_to?(:succ)}) {
return nil;
}
if (#{@begin}.$$is_string && #{@end}.$$is_string) {
return nil;
}
if (n % 1 === 0) {
return #{(size / n).ceil};
} else {
// n is a float
var begin = self.begin, end = self.end,
abs = Math.abs, floor = Math.floor,
err = (abs(begin) + abs(end) + abs(end - begin)) / abs(n) * #{::Float::EPSILON},
size;
if (err > 0.5) {
err = 0.5;
}
if (self.excl) {
size = floor((end - begin) / n - err);
if (size * n + begin < end) {
size++;
}
} else {
size = floor((end - begin) / n + err) + 1
}
return size;
}
}
}
unless block_given?
if (@begin.is_a?(Numeric) || @begin.nil?) &&
(@end.is_a?(Numeric) || @end.nil?) &&
!(@begin.nil? && @end.nil?)
return ::Enumerator::ArithmeticSequence.new(self, n, :step)
else
return enum_for(:step, n) do
%x{
coerceStepSize();
return enumeratorSize();
}
end
end
end
`coerceStepSize()`
if `self.begin.$$is_number && self.end.$$is_number`
i = 0
loop do
current = @begin + i * n
if @excl
break if current >= @end
elsif current > @end
break
end
yield(current)
i += 1
end
else
%x{
if (#{@begin}.$$is_string && #{@end}.$$is_string && n % 1 !== 0) {
#{::Kernel.raise ::TypeError, 'no implicit conversion to float from string'}
}
}
each_with_index do |value, idx|
yield(value) if idx % n == 0
end
end
self
end
|
41
42
43
44
|
# File 'opal/opal/corelib/range.rb', line 41
def to_a
::Kernel.raise ::TypeError, 'cannot convert endless range to an array' if `is_infinite(self)`
super
end
|
303
304
305
|
# File 'opal/opal/corelib/range.rb', line 303
def to_s
"#{@begin || ''}#{@excl ? '...' : '..'}#{@end || ''}"
end
|