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
|
# File 'opal/opal/corelib/range.rb', line 10
def initialize(first, last, exclude = false)
@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
16
17
18
19
20
21
22
23
24
25
26
|
# File 'opal/opal/corelib/range.rb', line 16
def ==(other)
%x{
if (!other._isRange) {
return false;
}
return self.exclude === other.exclude &&
self.begin == other.begin &&
self.end == other.end;
}
end
|
#===(value) ⇒ Object
Also known as:
cover?
28
29
30
|
# File 'opal/opal/corelib/range.rb', line 28
def ===(value)
@begin <= value && (@exclude ? value < @end : value <= @end)
end
|
#each {|current| ... } ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'opal/opal/corelib/range.rb', line 34
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
51
52
53
54
55
56
57
|
# File 'opal/opal/corelib/range.rb', line 51
def eql?(other)
return false unless Range === other
@exclude === other.exclude_end? &&
@begin.eql?(other.begin) &&
@end.eql?(other.end)
end
|
#exclude_end? ⇒ Boolean
59
60
61
|
# File 'opal/opal/corelib/range.rb', line 59
def exclude_end?
@exclude
end
|
#max ⇒ Object
FIXME: currently hardcoded to assume range holds numerics
70
71
72
73
74
75
76
|
# File 'opal/opal/corelib/range.rb', line 70
def max
if block_given?
super
else
`#@exclude ? #@end - 1 : #@end`
end
end
|
#min ⇒ Object
80
81
82
83
84
85
86
|
# File 'opal/opal/corelib/range.rb', line 80
def min
if block_given?
super
else
@begin
end
end
|
#size ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'opal/opal/corelib/range.rb', line 90
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
103
104
105
|
# File 'opal/opal/corelib/range.rb', line 103
def step(n = 1)
raise NotImplementedError
end
|
#to_s ⇒ Object
Also known as:
inspect
107
108
109
|
# File 'opal/opal/corelib/range.rb', line 107
def to_s
`#{@begin.inspect} + (#@exclude ? '...' : '..') + #{@end.inspect}`
end
|