Class: Integer
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::Numeric
|
|
Instance Chain:
self,
::Numeric,
::Comparable
|
|
Inherits: | Numeric |
Defined in: | numeric.c, rational.c |
Overview
Instance Attribute Summary
-
#even? ⇒ Boolean
readonly
Returns
true
ifint
is an even number. -
#integer? ⇒ Boolean
readonly
Since
int
is already anInteger
, this always returnstrue
. -
#odd? ⇒ Boolean
readonly
Returns
true
ifint
is an odd number.
::Numeric - Inherited
Instance Method Summary
-
#ceil ⇒ Integer
Alias for #to_i.
-
#chr([encoding]) ⇒ String
Returns a string containing the character represented by the
int
's value according toencoding
. -
#denominator ⇒ 1
Returns 1.
-
#downto(limit) {|i| ... } ⇒ self
Iterates the given block, passing decreasing values from
int
down to and includinglimit
. -
#floor ⇒ Integer
Alias for #to_i.
-
#gcd(int2) ⇒ Integer
Returns the greatest common divisor (always positive).
-
#gcdlcm(int2) ⇒ Array
Returns an array; [int.gcd(int2), int.lcm(int2)].
-
#lcm(int2) ⇒ Integer
Returns the least common multiple (always positive).
-
#next ⇒ Integer
(also: #succ)
Returns the
Integer
equal toint
+ 1, same asFixnum#next
. -
#numerator ⇒ self
Returns self.
-
#ord ⇒ self
Returns the
int
itself. -
#pred ⇒ Integer
Returns the
Integer
equal toint
- 1. -
#rationalize([eps]) ⇒ Rational
Returns the value as a rational.
-
#round([ndigits]) ⇒ Integer, Float
Rounds
int
to a given precision in decimal digits (default 0 digits). -
#succ ⇒ Integer
Alias for #next.
-
#times {|i| ... } ⇒ self
Iterates the given block
int
times, passing in values from zero toint - 1
. -
#to_i ⇒ Integer
(also: #to_int, #floor, #ceil, #truncate)
As
int
is already anInteger
, all these methods simply return the receiver. -
#to_int ⇒ Integer
Alias for #to_i.
-
#to_r ⇒ Rational
Returns the value as a rational.
-
#truncate ⇒ Integer
Alias for #to_i.
-
#upto(limit) {|i| ... } ⇒ self
Iterates the given block, passing in integer values from
int
up to and includinglimit
.
::Numeric - Inherited
#% | x.modulo(y) means x-y*(x/y).floor. |
#+@ | Unary Plus—Returns the receiver's value. |
#-@ | Unary Minus—Returns the receiver's value, negated. |
#<=> | Returns zero if |
#abs | Returns the absolute value of |
#abs2 | Returns square of self. |
#angle | Alias for Numeric#arg. |
#arg | Returns 0 if the value is positive, pi otherwise. |
#ceil | Returns the smallest possible |
#coerce | If a +numeric is the same type as |
#conj | Returns self. |
#conjugate | Alias for Numeric#conj. |
#denominator | Returns the denominator (always positive). |
#div | Uses |
#divmod | Returns an array containing the quotient and modulus obtained by dividing |
#eql? | Returns |
#fdiv | Returns float division. |
#floor | Returns the largest integer less than or equal to |
#i | Returns the corresponding imaginary number. |
#imag | Returns zero. |
#imaginary | Alias for Numeric#imag. |
#initialize_copy | Numerics are immutable values, which should not be copied. |
#magnitude | Alias for Numeric#abs. |
#modulo | Alias for Numeric#%. |
#numerator | Returns the numerator. |
#phase | Alias for Numeric#arg. |
#polar | Returns an array; [num.abs, num.arg]. |
#quo | Returns most exact division (rational for integers, float for floats). |
#rect | Returns an array; [num, 0]. |
#rectangular | Alias for Numeric#rect. |
#remainder | x.remainder(y) means x-y*(x/y).truncate. |
#round | Rounds |
#singleton_method_added | Trap attempts to add methods to ::Numeric objects. |
#step | Invokes the given block with the sequence of numbers starting at |
#to_c | Returns the value as a complex. |
#to_int | Invokes the child class's #to_i method to convert |
#truncate | Returns |
::Comparable - Included
#< | Compares two objects based on the receiver's |
#<= | Compares two objects based on the receiver's |
#== | Compares two objects based on the receiver's |
#> | Compares two objects based on the receiver's |
#>= | Compares two objects based on the receiver's |
#between? | Returns |
Instance Attribute Details
#even? ⇒ Boolean
(readonly)
Returns true
if int
is an even number.
#integer? ⇒ Boolean
(readonly)
Since int
is already an Integer
, this always returns true
.
#odd? ⇒ Boolean
(readonly)
Returns true
if int
is an odd number.
Instance Method Details
#to_i ⇒ Integer
#ceil ⇒ Integer
Integer
#ceil ⇒ Integer
Alias for #to_i.
#chr([encoding]) ⇒ String
Returns a string containing the character represented by the int
's value according to encoding
.
65.chr #=> "A"
230.chr #=> "\346"
255.chr(Encoding::UTF_8) #=> "\303\277"
#denominator ⇒ 1
Returns 1.
#downto(limit) {|i| ... } ⇒ self
#downto(limit) ⇒ Enumerator
self
#downto(limit) ⇒ Enumerator
Iterates the given block, passing decreasing values from int
down to and including limit
.
If no block is given, an ::Enumerator is returned instead.
5.downto(1) { |n| print n, ".. " }
print " Liftoff!\n"
#=> "5.. 4.. 3.. 2.. 1.. Liftoff!"
#to_i ⇒ Integer
#floor ⇒ Integer
Integer
#floor ⇒ Integer
Alias for #to_i.
#gcd(int2) ⇒ Integer
Returns the greatest common divisor (always positive). 0.gcd(x) and x.gcd(0) return abs(x).
2.gcd(2) #=> 2
3.gcd(-7) #=> 1
((1<<31)-1).gcd((1<<61)-1) #=> 1
#gcdlcm(int2) ⇒ Array
Returns an array; [int.gcd(int2), int.lcm(int2)].
2.gcdlcm(2) #=> [2, 2]
3.gcdlcm(-7) #=> [1, 21]
((1<<31)-1).gcdlcm((1<<61)-1) #=> [1, 4951760154835678088235319297]
#lcm(int2) ⇒ Integer
Returns the least common multiple (always positive). 0.lcm(x) and x.lcm(0) return zero.
2.lcm(2) #=> 2
3.lcm(-7) #=> 21
((1<<31)-1).lcm((1<<61)-1) #=> 4951760154835678088235319297
#next ⇒ Integer
#succ ⇒ Integer
Also known as: #succ
Integer
#succ ⇒ Integer
Returns the Integer
equal to int
+ 1, same as Fixnum#next
.
1.next #=> 2
(-1).next #=> 0
#numerator ⇒ self
Returns self.
#ord ⇒ self
Returns the int
itself.
?a.ord #=> 97
This method is intended for compatibility to character constant in Ruby 1.9.
For example, ?a.ord returns 97 both in 1.8 and 1.9.
#pred ⇒ Integer
Returns the Integer
equal to int
- 1.
1.pred #=> 0
(-1).pred #=> -2
#rationalize([eps]) ⇒ Rational
Returns the value as a rational. The optional argument eps is always ignored.
#round([ndigits]) ⇒ Integer
, Float
Rounds int
to a given precision in decimal digits (default 0 digits).
Precision may be negative. Returns a floating point number when ndigits
is positive, self
for zero, and round down for negative.
1.round #=> 1
1.round(2) #=> 1.0
15.round(-1) #=> 20
#next ⇒ Integer
#succ ⇒ Integer
Integer
#succ ⇒ Integer
Alias for #next.
#times {|i| ... } ⇒ self
#times ⇒ Enumerator
self
#times ⇒ Enumerator
Iterates the given block int
times, passing in values from zero to int - 1
.
If no block is given, an ::Enumerator is returned instead.
5.times do |i|
print i, " "
end
#=> 0 1 2 3 4
#to_i ⇒ Integer
Also known as: #to_int, #floor, #ceil, #truncate
#to_i ⇒ Integer
#to_int ⇒ Integer
Integer
#to_int ⇒ Integer
Alias for #to_i.
#to_r ⇒ Rational
Returns the value as a rational.
1.to_r #=> (1/1)
(1<<64).to_r #=> (18446744073709551616/1)
#to_i ⇒ Integer
#truncate ⇒ Integer
Integer
#truncate ⇒ Integer
Alias for #to_i.
#upto(limit) {|i| ... } ⇒ self
#upto(limit) ⇒ Enumerator
self
#upto(limit) ⇒ Enumerator
Iterates the given block, passing in integer values from int
up to and including limit
.
If no block is given, an ::Enumerator is returned instead.
For example:
5.upto(10) { |i| print i, " " }
#=> 5 6 7 8 9 10