Module: Integral

Included in:
Float, Integer
Defined in:
mrblib/numeric.rb

Overview

Integral

mruby special - module to share methods between Floats and Integers

to make them compatible

Instance Method Summary (collapse)

Instance Method Details

- (Object) downto(num, &block)

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_given?

  i = self.to_i
  while i >= num
    block.call(i)
    i -= 1
  end
  self
end

- (Object) next 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

- (Object) step(num, step = 1, &block)

Calls the given block from self to num incremented by step (default 1).

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'mrblib/numeric.rb', line 103

def step(num, step = 1, &block)
  raise ArgumentError, "step can't be 0" if step == 0
  return to_enum(:step, num, step) unless block_given?

  i = if num.kind_of? Float then self.to_f else 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

- (Object) times(&block)

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_given?

  i = 0
  while i < self
    block.call i
    i += 1
  end
  self
end

- (Object) upto(num, &block)

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_given?

  i = self.to_i
  while i <= num
    block.call(i)
    i += 1
  end
  self
end