diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2020-10-15 18:31:06 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-10-15 18:31:06 +0900 |
| commit | 9cebddf9fe83ae0acde6f64f291fa3c9fc22880f (patch) | |
| tree | 6f9ca4f2941c3da48a504c937719adca36e4cdfe /mrblib/numeric.rb | |
| parent | 8c276f95be2f4e9deed73f08125a23a6746cb517 (diff) | |
| parent | 21e07d61138a87891dc780efaa28e6c76a39378f (diff) | |
| download | mruby-9cebddf9fe83ae0acde6f64f291fa3c9fc22880f.tar.gz mruby-9cebddf9fe83ae0acde6f64f291fa3c9fc22880f.zip | |
Merge pull request #5084 from mruby/mruby3
Mruby3
Diffstat (limited to 'mrblib/numeric.rb')
| -rw-r--r-- | mrblib/numeric.rb | 47 |
1 files changed, 36 insertions, 11 deletions
diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index 5926518d5..e28d63324 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -34,11 +34,11 @@ class Numeric end ## -# Integral +# Integer # -# mruby special - module to share methods between Floats and Integers -# to make them compatible -module Integral +# ISO 15.2.8 +## +class Integer ## # Calls the given block once for each Integer # from +self+ downto +num+. @@ -125,14 +125,7 @@ module Integral end self end -end -## -# Integer -# -# ISO 15.2.8 -class Integer - include Integral ## # Returns the receiver simply. # @@ -161,3 +154,35 @@ class Integer # ISO 15.2.8.3.26 alias truncate floor end + +class Float + ## + # Calls the given block from +self+ to +num+ + # incremented by +step+ (default 1). + # + 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 + + i = self + if num == self || step.infinite? + block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i) + elsif num == nil + while true + block.call(i) + i += step + end + elsif step > 0 + while i <= num + block.call(i) + i += step + end + else + while i >= num + block.call(i) + i += step + end + end + self + end +end |
