Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
mrblib/numeric.rb,
mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb

Overview

Integer

ISO 15.2.8

Instance Method Summary collapse

Instance Method Details

#allbits?(mask) ⇒ Boolean

call-seq: int.allbits?(mask) -> true or false

Returns true if all bits of +int+ & +mask+ are 1.

Returns:

  • (Boolean)


71
72
73
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 71

def allbits?(mask)
  (self & mask) == mask
end

#anybits?(mask) ⇒ Boolean

call-seq: int.anybits?(mask) -> true or false

Returns true if any bits of +int+ & +mask+ are 1.

Returns:

  • (Boolean)


81
82
83
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 81

def anybits?(mask)
  (self & mask) != 0
end

#ceildiv(other) ⇒ Object

call-seq: ceildiv(other) -> integer

Returns the result of division self by other. The result is rounded up to the nearest integer.

3.ceildiv(3) # => 1
4.ceildiv(3) # => 2

4.ceildiv(-3) # => -1
-4.ceildiv(3) # => -1
-4.ceildiv(-3) # => 2

3.ceildiv(1.2) # => 3


109
110
111
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 109

def ceildiv(other)
  -div(-other)
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

#integer?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 113

def integer?
  true
end

#nextObject 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

#nobits?(mask) ⇒ Boolean

call-seq: int.nobits?(mask) -> true or false

Returns true if no bits of +int+ & +mask+ are 1.

Returns:

  • (Boolean)


91
92
93
# File 'mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb', line 91

def nobits?(mask)
  (self & mask) == 0
end

#step(num = nil, step = 1, &block) ⇒ Object

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
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 = __coerce_step_counter(step)
  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

#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