Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3f7eb47

Browse files
committedApr 25, 2014
WIP
1 parent 3b7ccbc commit 3f7eb47

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed
 

‎opal/corelib/numeric.rb

+4
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ def positive?
496496
def negative?
497497
`1 / self < 0`
498498
end
499+
500+
def self.===(other)
501+
`!!other._isNumber`
502+
end
499503
end
500504

501505
Fixnum = Numeric

‎opal/corelib/range.rb

+47-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,53 @@ def size
100100
(`Math.abs(_end - _begin) + 1`).to_i
101101
end
102102

103-
def step(n = 1)
104-
raise NotImplementedError
103+
def step(step = 1)
104+
return enum_for :step, step unless block_given?
105+
106+
_begin = @begin
107+
_end = @end
108+
current = _begin
109+
110+
p ['>>>>', inspect, step]
111+
112+
# if Numeric === _begin && (Float === step && !(Integer === step))
113+
p ['Numeric === _begin', Numeric === _begin]
114+
if Numeric === _begin
115+
unless Numeric === step
116+
step = Opal.coerce_to step, Integer, :to_int
117+
raise TypeError, "step's not a number" unless Numeric === step
118+
end
119+
else
120+
step = Opal.coerce_to step, Integer, :to_int
121+
raise TypeError, "step's not an Integer" unless Integer === step
122+
# raise TypeError, "step's not a number" unless Float === step
123+
end
124+
125+
raise ArgumentError, "step can't be negative" if step < 0
126+
raise ArgumentError, "step can't be zero" if step == 0
127+
128+
if Numeric === _begin && Numeric === _end && Numeric === step # fixnums are special
129+
_end += step / 2.0 unless @exclude
130+
%x{
131+
while (current < _end) {
132+
#{yield(current)};
133+
current += step;
134+
}
135+
}
136+
else
137+
raise TypeError, 'range begin does not respond to #succ' unless _begin.respond_to? :succ
138+
_end = _end.succ unless @exclude
139+
yield current
140+
until `((#{current <=> _end}) < 0)`
141+
%x{
142+
for (var i = step; i > 0; i--) {
143+
#{current = current.succ};
144+
}
145+
}
146+
yield current
147+
end
148+
end
149+
self
105150
end
106151

107152
def to_s

‎spec/filters/unsupported/float.rb

+4
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
fails "Array#inspect represents a recursive element with '[...]'"
33
fails "Array#to_s represents a recursive element with '[...]'"
44
fails "Array#eql? returns false if any corresponding elements are not #eql?"
5+
6+
# The spec uses the float "2.0" which is undistiguishable from an Integer
7+
fails "Range#step with exclusive end and String values raises a TypeError when passed a Float step"
8+
fails "Range#step with inclusive end and String values raises a TypeError when passed a Float step"
59
end

‎spec/rubyspecs

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ corelib/core/matchdata/to_a_spec
4747

4848
corelib/core/range/begin_spec
4949
corelib/core/range/end_spec
50+
corelib/core/range/step_spec
5051
corelib/core/range/size_spec
5152

5253
corelib/core/string/allocate_spec

0 commit comments

Comments
 (0)
Please sign in to comment.