diff options
| author | Felix Jones <[email protected]> | 2017-02-16 13:33:46 +0000 |
|---|---|---|
| committer | Felix Jones <[email protected]> | 2017-02-16 13:33:46 +0000 |
| commit | d83aad8d570e4bbffa3bd3ce64e210f78afa425f (patch) | |
| tree | 5389a87c135b1bdf3e23a1ba02e02400b7cf80fc /mrblib | |
| parent | 70aa6dc38d75dd6b1e2c76f290bc576e36e36ea3 (diff) | |
| parent | b165708c8deba00685f9a27926c554aaa7f3b0fb (diff) | |
| download | mruby-d83aad8d570e4bbffa3bd3ce64e210f78afa425f.tar.gz mruby-d83aad8d570e4bbffa3bd3ce64e210f78afa425f.zip | |
Merge branch 'master' into android.rake-ndk-clang
Diffstat (limited to 'mrblib')
| -rw-r--r-- | mrblib/numeric.rb | 39 | ||||
| -rw-r--r-- | mrblib/string.rb | 14 |
2 files changed, 20 insertions, 33 deletions
diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index 6e4c5027f..975ad973f 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -100,11 +100,18 @@ module Integral # Calls the given block from +self+ to +num+ # incremented by +step+ (default 1). # - def step(num, step = 1, &block) + def step(num=nil, step=1, &block) raise ArgumentError, "step can't be 0" if step == 0 return to_enum(:step, num, step) unless block_given? i = if num.kind_of? Float then self.to_f else self end + if num == nil + while true + block.call(i) + i+=step + end + return self + end if step > 0 while i <= num block.call(i) @@ -160,35 +167,7 @@ end # # ISO 15.2.9 class Float - include Integral # mruby special - since mruby integers may be upgraded to floats, # floats should be compatible to integers. - def >> other - n = self.to_i - other = other.to_i - if other < 0 - n << -other - else - other.times { n /= 2 } - if n.abs < 1 - if n >= 0 - 0 - else - -1 - end - else - n.to_i - end - end - end - def << other - n = self.to_i - other = other.to_i - if other < 0 - n >> -other - else - other.times { n *= 2 } - n - end - end + include Integral end diff --git a/mrblib/string.rb b/mrblib/string.rb index 37441ec98..aa2ca9973 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -159,16 +159,24 @@ class String anum = args.size if anum == 2 pos, value = args - if pos.kind_of? String + case pos + when String posnum = self.index(pos) if posnum b = self[0, posnum.to_i] a = self[(posnum + pos.length)..-1] self.replace([b, value, a].join('')) - return value else raise IndexError, "string not matched" end + when Range + head = pos.begin + tail = pos.end + tail += self.length if tail < 0 + unless pos.exclude_end? + tail += 1 + end + return self[head, tail-head]=value else pos += self.length if pos < 0 if pos < 0 || pos > self.length @@ -177,8 +185,8 @@ class String b = self[0, pos.to_i] a = self[pos + 1..-1] self.replace([b, value, a].join('')) - return value end + return value elsif anum == 3 pos, len, value = args pos += self.length if pos < 0 |
