diff options
| author | Jun Hiroe <[email protected]> | 2014-12-27 18:09:36 +0900 |
|---|---|---|
| committer | Jun Hiroe <[email protected]> | 2015-01-13 16:32:30 +0900 |
| commit | e9980942976ccae658a9e0c212ccebb3a0fa7f8a (patch) | |
| tree | 130e463deace2b961fd8e2f9b5e0906ae01e4225 /mrbgems | |
| parent | 4aef1ad209179e67bbeb9d3bfaad2f5d233d958e (diff) | |
| download | mruby-e9980942976ccae658a9e0c212ccebb3a0fa7f8a.tar.gz mruby-e9980942976ccae658a9e0c212ccebb3a0fa7f8a.zip | |
Add String#upto
Diffstat (limited to 'mrbgems')
| -rw-r--r-- | mrbgems/mruby-string-ext/mrblib/string.rb | 43 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/test/string.rb | 62 |
2 files changed, 105 insertions, 0 deletions
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index fd805b144..2b61fdb48 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -267,4 +267,47 @@ class String end return newstr.slice(0,idx) end + + # str.upto(other_str, exclusive=false) {|s| block } -> str + # str.upto(other_str, exclusive=false) -> an_enumerator + # + # Iterates through successive values, starting at <i>str</i> and + # ending at <i>other_str</i> inclusive, passing each value in turn to + # the block. The <code>String#succ</code> method is used to generate + # each value. If optional second argument exclusive is omitted or is false, + # the last value will be included; otherwise it will be excluded. + # + # If no block is given, an enumerator is returned instead. + # + # "a8".upto("b6") {|s| print s, ' ' } + # for s in "a8".."b6" + # print s, ' ' + # end + # + # <em>produces:</em> + # + # a8 a9 b0 b1 b2 b3 b4 b5 b6 + # a8 a9 b0 b1 b2 b3 b4 b5 b6 + # + # If <i>str</i> and <i>other_str</i> contains only ascii numeric characters, + # both are recognized as decimal numbers. In addition, the width of + # string (e.g. leading zeros) is handled appropriately. + # + # "9".upto("11").to_a #=> ["9", "10", "11"] + # "25".upto("5").to_a #=> [] + # "07".upto("11").to_a #=> ["07", "08", "09", "10", "11"] + # + def upto(other_str, excl=false, &block) + return to_enum :upto, other_str, excl unless block + + str = self + n = self.<=>other_str + return self if n > 0 || (self == other_str && excl) + while true + block.call(str) + return self if !excl && str == other_str + str = str.succ + return self if excl && str == other_str + end + end end diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb index 83e69859b..14e00428e 100644 --- a/mrbgems/mruby-string-ext/test/string.rb +++ b/mrbgems/mruby-string-ext/test/string.rb @@ -393,3 +393,65 @@ assert('String#ljust') do assert_equal "hello123412341234123", "hello".ljust(20, '1234') assert_equal "hello", "hello".ljust(-3) end + +assert('String#upto') do + a = "aa" + start = "aa" + count = 0 + assert_equal("aa", a.upto("zz") {|s| + assert_equal(start, s) + start.succ! + count += 1 + }) + assert_equal(676, count) + + a = "a" + start = "a" + count = 0 + assert_equal("a", a.upto("a") {|s| + assert_equal(start, s) + start.succ! + count += 1 + }) + assert_equal(1, count) + + a = "a" + start = "a" + count = 0 + assert_equal("a", a.upto("b", true) {|s| + assert_equal(start, s) + start.succ! + count += 1 + }) + assert_equal(1, count) + + a = "0" + start = "0" + count = 0 + assert_equal("0", a.upto("0") {|s| + assert_equal(start, s) + start.succ! + count += 1 + }) + assert_equal(1, count) + + a = "0" + start = "0" + count = 0 + assert_equal("0", a.upto("-1") {|s| + assert_equal(start, s) + start.succ! + count += 1 + }) + assert_equal(0, count) + + a = "-1" + start = "-1" + count = 0 + assert_equal("-1", a.upto("-2") {|s| + assert_equal(start, s) + start.succ! + count += 1 + }) + assert_equal(2, count) +end |
