diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2020-08-26 17:53:04 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2020-10-12 18:19:54 +0900 |
| commit | 2b188ed8a191257f23ddf6f8a27bf1d3964587ed (patch) | |
| tree | bccc235fdf9088a5486201c3855bfb2bfe04a1f2 /mrblib | |
| parent | 2bb84f1f1ae4bfca49bd92d8d0102a5773d3270f (diff) | |
| download | mruby-2b188ed8a191257f23ddf6f8a27bf1d3964587ed.tar.gz mruby-2b188ed8a191257f23ddf6f8a27bf1d3964587ed.zip | |
Reorganize `Integer` system.
- Integrate `Fixnum` and `Integer`
- Remove `Integral`
- `int / int -> int`
- Replace `mrb_fixnum()` to `mrb_int()`
- Replace `mrb_fixnum_value()` to `mrb_int_value()`.
- Use `mrb_integer_p()` instead of `mrb_fixnum_p()`
Diffstat (limited to 'mrblib')
| -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 |
