Permalink
Browse files

Use string#upto() if available; fix #3489

Terminate loop if the value is longer than the last otherwise.
  • Loading branch information...
matz committed Mar 10, 2017
1 parent 4763312 commit c6e5659fec42b1c2ac39292a1ba2ee5852c66846
Showing with 11 additions and 0 deletions.
  1. +11 −0 mrblib/range.rb
View
@@ -26,13 +26,24 @@ def each(&block)
return self
end
if val.kind_of?(String) && last.kind_of?(String) # fixnums are special
if val.respond_to? :upto
return val.upto(last, exclude_end?, &block)
else
str_each = true
end
end
raise TypeError, "can't iterate" unless val.respond_to? :succ
return self if (val <=> last) > 0
while (val <=> last) < 0
block.call(val)
val = val.succ
if str_each
break if val.size > last.size
end
end
block.call(val) if !exclude_end? && (val <=> last) == 0

0 comments on commit c6e5659

Please sign in to comment.