diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-03-10 10:39:43 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-03-10 10:39:43 +0900 |
| commit | c6e5659fec42b1c2ac39292a1ba2ee5852c66846 (patch) | |
| tree | 79b75bd11f260e8e6d2174fbc97d58eaf5226815 /mrblib | |
| parent | 4763312fb8f9c88c3fa7eb7f8710ad6fbd5ed1b4 (diff) | |
| download | mruby-c6e5659fec42b1c2ac39292a1ba2ee5852c66846.tar.gz mruby-c6e5659fec42b1c2ac39292a1ba2ee5852c66846.zip | |
Use string#upto() if available; fix #3489
Terminate loop if the value is longer than the last otherwise.
Diffstat (limited to 'mrblib')
| -rw-r--r-- | mrblib/range.rb | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/mrblib/range.rb b/mrblib/range.rb index 5e5fd9bdc..3322af5f1 100644 --- a/mrblib/range.rb +++ b/mrblib/range.rb @@ -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 |
