Class: Range
Instance Attribute Summary collapse
-
#begin ⇒ Object
(also: #first)
readonly
Returns the value of attribute begin.
-
#end ⇒ Object
(also: #last)
readonly
Returns the value of attribute end.
Instance Method Summary
collapse
Methods included from Enumerable
#all?, #any?, #chunk, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #find_all, #find_index, #grep, #group_by, #inject, #lazy, #max_by, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #slice_before, #sort, #sort_by, #take, #take_while, #zip
Constructor Details
#initialize(first, last, exclude = false) ⇒ Range
Returns a new instance of Range
10
11
12
13
14
15
16
|
# File 'opal/opal/corelib/range.rb', line 10
def initialize(first, last, exclude = false)
raise ArgumentError unless first <=> last
@begin = first
@end = last
@exclude = exclude
end
|
Instance Attribute Details
#begin ⇒ Object
Also known as:
first
Returns the value of attribute begin
8
9
10
|
# File 'opal/opal/corelib/range.rb', line 8
def begin
@begin
end
|
#end ⇒ Object
Also known as:
last
Returns the value of attribute end
8
9
10
|
# File 'opal/opal/corelib/range.rb', line 8
def end
@end
end
|
Instance Method Details
#==(other) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
|
# File 'opal/opal/corelib/range.rb', line 18
def ==(other)
%x{
if (!other.$$is_range) {
return false;
}
return self.exclude === other.exclude &&
self.begin == other.begin &&
self.end == other.end;
}
end
|
#===(value) ⇒ Object
30
31
32
|
# File 'opal/opal/corelib/range.rb', line 30
def ===(value)
include? value
end
|
#cover?(value) ⇒ Boolean
Also known as:
include?
34
35
36
|
# File 'opal/opal/corelib/range.rb', line 34
def cover?(value)
@begin <= value && (@exclude ? value < @end : value <= @end)
end
|
#each {|current| ... } ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'opal/opal/corelib/range.rb', line 38
def each(&block)
return enum_for :each unless block_given?
current = @begin
last = @end
while current < last
yield current
current = current.succ
end
yield current if !@exclude && current == last
self
end
|
#eql?(other) ⇒ Boolean
55
56
57
58
59
60
61
|
# File 'opal/opal/corelib/range.rb', line 55
def eql?(other)
return false unless Range === other
@exclude === other.exclude_end? &&
@begin.eql?(other.begin) &&
@end.eql?(other.end)
end
|
#exclude_end? ⇒ Boolean
63
64
65
|
# File 'opal/opal/corelib/range.rb', line 63
def exclude_end?
@exclude
end
|
#max ⇒ Object
FIXME: currently hardcoded to assume range holds numerics
74
75
76
77
78
79
80
|
# File 'opal/opal/corelib/range.rb', line 74
def max
if block_given?
super
else
`#@exclude ? #@end - 1 : #@end`
end
end
|
#min ⇒ Object
84
85
86
87
88
89
90
|
# File 'opal/opal/corelib/range.rb', line 84
def min
if block_given?
super
else
@begin
end
end
|
#size ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'opal/opal/corelib/range.rb', line 94
def size
_begin = @begin
_end = @end
_end -= 1 if @exclude
return nil unless Numeric === _begin && Numeric === _end
return 0 if _end < _begin
infinity = Float::INFINITY
return infinity if infinity == _begin.abs || _end.abs == infinity
(`Math.abs(_end - _begin) + 1`).to_i
end
|
#step(n = 1) ⇒ Object
107
108
109
|
# File 'opal/opal/corelib/range.rb', line 107
def step(n = 1)
raise NotImplementedError
end
|
#to_s ⇒ Object
Also known as:
inspect
111
112
113
|
# File 'opal/opal/corelib/range.rb', line 111
def to_s
`#{@begin.inspect} + (#@exclude ? '...' : '..') + #{@end.inspect}`
end
|