diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-10-22 16:18:11 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-10-22 16:18:11 +0900 |
| commit | 2607970ed9ab59165d9532c05440fb57846d636e (patch) | |
| tree | 7d95b33c858762f4473b78438af9aba9eb92f32e /mrblib | |
| parent | 620f8b2721e2bf05b610116cc3857a87b6c140e6 (diff) | |
| download | mruby-2607970ed9ab59165d9532c05440fb57846d636e.tar.gz mruby-2607970ed9ab59165d9532c05440fb57846d636e.zip | |
move some methods to make floats and integers compatible [mruby special]
Diffstat (limited to 'mrblib')
| -rw-r--r-- | mrblib/numeric.rb | 104 |
1 files changed, 63 insertions, 41 deletions
diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index f3f1a2a7e..9c799df5a 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -3,6 +3,7 @@ # # ISO 15.2.7 class Numeric + include Comparable ## # Returns the receiver simply. # @@ -33,19 +34,11 @@ class Numeric end ## -# Integer +# Integral # -# ISO 15.2.8 -class Integer - - ## - # Returns the receiver simply. - # - # ISO 15.2.8.3.14 - def ceil - self - end - +# mruby special - module to share methods between Floats and Integers +# to make them compatible +module Integral ## # Calls the given block once for each Integer # from +self+ downto +num+. @@ -61,14 +54,6 @@ class Integer end ## - # Returns the receiver simply. - # - # ISO 15.2.8.3.17 - def floor - self - end - - ## # Returns self + 1 # # ISO 15.2.8.3.19 @@ -92,22 +77,6 @@ class Integer end ## - # Returns the receiver simply. - # - # ISO 15.2.8.3.24 - def round - self - end - - ## - # Returns the receiver simply. - # - # ISO 15.2.8.3.26 - def truncate - self - end - - ## # Calls the given block once for each Integer # from +self+ upto +num+. # @@ -136,10 +105,63 @@ class Integer end ## -# Numeric is comparable +# Integer # -# ISO 15.2.7.3 -module Comparable; end -class Numeric - include Comparable +# ISO 15.2.8 +class Integer + include Integral + ## + # Returns the receiver simply. + # + # ISO 15.2.8.3.14 + def ceil + self + end + + ## + # Returns the receiver simply. + # + # ISO 15.2.8.3.17 + def floor + self + end + + ## + # Returns the receiver simply. + # + # ISO 15.2.8.3.24 + alias round floor + + ## + # Returns the receiver simply. + # + # ISO 15.2.8.3.26 + alias truncate floor +end + +## +# Float +# +# 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.to_i.times { + n /= 2 + } + n + end + def << other + n = self.to_i + other.to_i.times { + n *= 2 + } + n.to_i + end + + def divmod(other) + end end |
