diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:19:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:20:02 +0000 |
commit | 58daab21cd043e1dc37024a7f99b396788372918 (patch) | |
tree | 96771e43bb69f7c1c2b0b4f7374cb74d7866d0cb /web/server/h2o/libh2o/deps/mruby/mrblib/numeric.rb | |
parent | Releasing debian version 1.43.2-1. (diff) | |
download | netdata-58daab21cd043e1dc37024a7f99b396788372918.tar.xz netdata-58daab21cd043e1dc37024a7f99b396788372918.zip |
Merging upstream version 1.44.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'web/server/h2o/libh2o/deps/mruby/mrblib/numeric.rb')
-rw-r--r-- | web/server/h2o/libh2o/deps/mruby/mrblib/numeric.rb | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/web/server/h2o/libh2o/deps/mruby/mrblib/numeric.rb b/web/server/h2o/libh2o/deps/mruby/mrblib/numeric.rb new file mode 100644 index 000000000..89401a084 --- /dev/null +++ b/web/server/h2o/libh2o/deps/mruby/mrblib/numeric.rb @@ -0,0 +1,173 @@ +## +# Numeric +# +# ISO 15.2.7 +class Numeric + include Comparable + ## + # Returns the receiver simply. + # + # ISO 15.2.7.4.1 + def +@ + self + end + + ## + # Returns the receiver's value, negated. + # + # ISO 15.2.7.4.2 + def -@ + 0 - self + end + + ## + # Returns the absolute value of the receiver. + # + # ISO 15.2.7.4.3 + def abs + if self < 0 + -self + else + self + end + end +end + +## +# Integral +# +# 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+. + # + # ISO 15.2.8.3.15 + def downto(num, &block) + return to_enum(:downto, num) unless block + + i = self.to_i + while i >= num + block.call(i) + i -= 1 + end + self + end + + ## + # Returns self + 1 + # + # ISO 15.2.8.3.19 + def next + self + 1 + end + # ISO 15.2.8.3.21 + alias succ next + + ## + # Calls the given block +self+ times. + # + # ISO 15.2.8.3.22 + def times &block + return to_enum :times unless block + + i = 0 + while i < self + block.call i + i += 1 + end + self + end + + ## + # Calls the given block once for each Integer + # from +self+ upto +num+. + # + # ISO 15.2.8.3.27 + def upto(num, &block) + return to_enum(:upto, num) unless block + + i = self.to_i + while i <= num + block.call(i) + i += 1 + end + self + end + + ## + # 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 = 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) + i += step + end + else + while i >= num + block.call(i) + i += step + end + end + self + end +end + +## +# Integer +# +# 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 + # mruby special - since mruby integers may be upgraded to floats, + # floats should be compatible to integers. + include Integral +end |