Module: Integral
- Defined in:
- mrblib/numeric.rb,
mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb
Overview
Integral
mruby special - module to share methods between Floats and Integers to make them compatible
Instance Method Summary collapse
-
#div(other) ⇒ Object
-
#downto(num, &block) ⇒ Object
Calls the given block once for each Integer from +self+ downto +num+.
-
#negative? ⇒ Boolean
-
#next ⇒ Object
(also: #succ)
Returns self + 1.
-
#nonzero? ⇒ Boolean
-
#positive? ⇒ Boolean
-
#step(num = nil, step = 1, &block) ⇒ Object
Calls the given block from +self+ to +num+ incremented by +step+ (default 1).
-
#times(&block) ⇒ Object
Calls the given block +self+ times.
-
#upto(num, &block) ⇒ Object
Calls the given block once for each Integer from +self+ upto +num+.
-
#zero? ⇒ Boolean
Instance Method Details
#div(other) ⇒ Object
2 3 4 |
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 2 def div(other) self.divmod(other)[0] end |
#downto(num, &block) ⇒ Object
Calls the given block once for each Integer from +self+ downto +num+.
ISO 15.2.8.3.15
47 48 49 50 51 52 53 54 55 56 |
# File 'mrblib/numeric.rb', line 47 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 |
#negative? ⇒ Boolean
22 23 24 |
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 22 def negative? self < 0 end |
#next ⇒ Object Also known as: succ
Returns self + 1
ISO 15.2.8.3.19
62 63 64 |
# File 'mrblib/numeric.rb', line 62 def next self + 1 end |
#nonzero? ⇒ Boolean
10 11 12 13 14 15 16 |
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 10 def nonzero? if self == 0 nil else self end end |
#positive? ⇒ Boolean
18 19 20 |
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 18 def positive? self > 0 end |
#step(num = nil, step = 1, &block) ⇒ Object
Calls the given block from +self+ to +num+ incremented by +step+ (default 1).
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'mrblib/numeric.rb', line 103 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 class_defined?("Float") && 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 |
#times(&block) ⇒ Object
Calls the given block +self+ times.
ISO 15.2.8.3.22
72 73 74 75 76 77 78 79 80 81 |
# File 'mrblib/numeric.rb', line 72 def times &block return to_enum :times unless block i = 0 while i < self block.call i i += 1 end self end |
#upto(num, &block) ⇒ Object
Calls the given block once for each Integer from +self+ upto +num+.
ISO 15.2.8.3.27
88 89 90 91 92 93 94 95 96 97 |
# File 'mrblib/numeric.rb', line 88 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 |
#zero? ⇒ Boolean
6 7 8 |
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 6 def zero? self == 0 end |