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

Terminate loop if the value is longer than the last otherwise.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-03-10 10:39:43 +09:00
parent 4763312fb8
commit c6e5659fec
+11
View File
@@ -26,6 +26,14 @@ class Range
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
@@ -33,6 +41,9 @@ class Range
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