# Class: Integer

Inherits:
Numeric
show all
Includes:
Precision
Defined in:
numeric.c

## Overview

Integer is the basis for the two concrete classes that hold whole numbers, Bignum and Fixnum.

## Class Method Summary (collapse)

• Convert obj to an Integer.

## Instance Method Summary (collapse)

• As int is already an Integer, all these methods simply return the receiver.

• Returns a string containing the ASCII character represented by the receiver's value.

• Iterates block, passing decreasing values from int down to and including limit.

• Returns true if int is an even number.

• As int is already an Integer, all these methods simply return the receiver.

• Always returns true.

• Returns the Integer equal to int + 1.

• Returns true if int is an odd number.

• Returns the int itself.

• Returns the Integer equal to int - 1.

• As int is already an Integer, all these methods simply return the receiver.

• Returns the Integer equal to int + 1.

• Iterates block int times, passing in values from zero to int - 1.

• As int is already an Integer, all these methods simply return the receiver.

• As int is already an Integer, all these methods simply return the receiver.

• As int is already an Integer, all these methods simply return the receiver.

• Iterates block, passing in integer values from int up to and including limit.

## Class Method Details

### + (Fixnum) induced_from(obj)

Convert obj to an Integer.

Returns:

 ``` ``` ```# File 'numeric.c' /* * call-seq: * Integer.induced_from(obj) => fixnum, bignum * * Convert obj to an Integer. */ static VALUE rb_int_induced_from(klass, x) VALUE klass, x; { switch (TYPE(x)) { case T_FIXNUM: case T_BIGNUM: return x; case T_FLOAT: return rb_funcall(x, id_to_i, 0); default: rb_raise(rb_eTypeError, "failed to convert %s into Integer", rb_obj_classname(x)); } }```

## Instance Method Details

### - (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) round - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.to_i => int * int.to_int => int * int.floor => int * int.ceil => int * int.round => int * int.truncate => int * * As int is already an Integer, all these * methods simply return the receiver. */ static VALUE int_to_i(num) VALUE num; { return num; }```

### - (String) chr

Returns a string containing the ASCII character represented by the receiver's value.

``````65.chr    #=> "A"
?a.chr    #=> "a"
230.chr   #=> "\346"``````

Returns:

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.chr => string * * Returns a string containing the ASCII character represented by the * receiver's value. * * 65.chr #=> "A" * ?a.chr #=> "a" * 230.chr #=> "\346" */ static VALUE int_chr(num) VALUE num; { char c; long i = NUM2LONG(num); if (i < 0 || 0xff < i) rb_raise(rb_eRangeError, "%ld out of char range", i); c = i; return rb_str_new(&c, 1); }```

### - (Integer) downto(limit) {|i| ... }

Iterates block, passing decreasing values from int down to and including limit.

``````5.downto(1) { |n| print n, ".. " }
print "  Liftoff!\n"``````

produces:

``5.. 4.. 3.. 2.. 1..   Liftoff!``

Yields:

• (i)

Returns:

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.downto(limit) {|i| block } => int * * Iterates block, passing decreasing values from int * down to and including limit. * * 5.downto(1) { |n| print n, ".. " } * print " Liftoff!\n" * * produces: * * 5.. 4.. 3.. 2.. 1.. Liftoff! */ static VALUE int_downto(from, to) VALUE from, to; { RETURN_ENUMERATOR(from, 1, &to); if (FIXNUM_P(from) && FIXNUM_P(to)) { long i, end; end = FIX2LONG(to); for (i=FIX2LONG(from); i >= end; i--) { rb_yield(LONG2FIX(i)); } } else { VALUE i = from, c; while (!(c = rb_funcall(i, '<', 1, to))) { rb_yield(i); i = rb_funcall(i, '-', 1, INT2FIX(1)); } if (NIL_P(c)) rb_cmperr(i, to); } return from; }```

### - (Boolean) even?

Returns true if int is an even number.

Returns:

• (Boolean)
 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.even? -> true or false * * Returns true if int is an even number. */ static VALUE int_even_p(VALUE num) { if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) { return Qtrue; } return Qfalse; }```

### - (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) round - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.to_i => int * int.to_int => int * int.floor => int * int.ceil => int * int.round => int * int.truncate => int * * As int is already an Integer, all these * methods simply return the receiver. */ static VALUE int_to_i(num) VALUE num; { return num; }```

### - (true) integer?

Always returns true.

Returns:

• (true)
 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.integer? -> true * * Always returns true. */ static VALUE int_int_p(num) VALUE num; { return Qtrue; }```

### - (Integer) next - (Integer) succ

Returns the Integer equal to int + 1.

``````1.next      #=> 2
(-1).next   #=> 0``````

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.next => integer * int.succ => integer * * Returns the Integer equal to int + 1. * * 1.next #=> 2 * (-1).next #=> 0 */ static VALUE int_succ(num) VALUE num; { if (FIXNUM_P(num)) { long i = FIX2LONG(num) + 1; return LONG2NUM(i); } return rb_funcall(num, '+', 1, INT2FIX(1)); }```

### - (Boolean) odd?

Returns true if int is an odd number.

Returns:

• (Boolean)
 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.odd? -> true or false * * Returns true if int is an odd number. */ static VALUE int_odd_p(VALUE num) { if (rb_funcall(num, '%', 1, INT2FIX(2)) != INT2FIX(0)) { return Qtrue; } return Qfalse; }```

### - (Integer) ord

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.

Returns:

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.ord => int * * 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. */ static VALUE int_ord(num) VALUE num; { return num; }```

### - (Integer) pred

Returns the Integer equal to int - 1.

``````1.pred      #=> 0
(-1).pred   #=> -2``````

Returns:

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.pred => integer * * Returns the Integer equal to int - 1. * * 1.pred #=> 0 * (-1).pred #=> -2 */ static VALUE int_pred(VALUE num) { if (FIXNUM_P(num)) { long i = FIX2LONG(num) - 1; return LONG2NUM(i); } return rb_funcall(num, '-', 1, INT2FIX(1)); }```

### - (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) round - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.to_i => int * int.to_int => int * int.floor => int * int.ceil => int * int.round => int * int.truncate => int * * As int is already an Integer, all these * methods simply return the receiver. */ static VALUE int_to_i(num) VALUE num; { return num; }```

### - (Integer) next - (Integer) succ

Returns the Integer equal to int + 1.

``````1.next      #=> 2
(-1).next   #=> 0``````

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.next => integer * int.succ => integer * * Returns the Integer equal to int + 1. * * 1.next #=> 2 * (-1).next #=> 0 */ static VALUE int_succ(num) VALUE num; { if (FIXNUM_P(num)) { long i = FIX2LONG(num) + 1; return LONG2NUM(i); } return rb_funcall(num, '+', 1, INT2FIX(1)); }```

### - (Integer) times {|i| ... }

Iterates block int times, passing in values from zero to int - 1.

``````5.times do |i|
print i, " "
end``````

produces:

``0 1 2 3 4``

Yields:

• (i)

Returns:

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.times {|i| block } => int * * Iterates block int times, passing in values from zero to * int - 1. * * 5.times do |i| * print i, " " * end * * produces: * * 0 1 2 3 4 */ static VALUE int_dotimes(num) VALUE num; { RETURN_ENUMERATOR(num, 0, 0); if (FIXNUM_P(num)) { long i, end; end = FIX2LONG(num); for (i=0; i

### - (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) round - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.to_i => int * int.to_int => int * int.floor => int * int.ceil => int * int.round => int * int.truncate => int * * As int is already an Integer, all these * methods simply return the receiver. */ static VALUE int_to_i(num) VALUE num; { return num; }```

### - (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) round - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.to_i => int * int.to_int => int * int.floor => int * int.ceil => int * int.round => int * int.truncate => int * * As int is already an Integer, all these * methods simply return the receiver. */ static VALUE int_to_i(num) VALUE num; { return num; }```

### - (Integer) to_i - (Integer) to_int - (Integer) floor - (Integer) ceil - (Integer) round - (Integer) truncate

As int is already an Integer, all these methods simply return the receiver.

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.to_i => int * int.to_int => int * int.floor => int * int.ceil => int * int.round => int * int.truncate => int * * As int is already an Integer, all these * methods simply return the receiver. */ static VALUE int_to_i(num) VALUE num; { return num; }```

### - (Integer) upto(limit) {|i| ... }

Iterates block, passing in integer values from int up to and including limit.

``5.upto(10) { |i| print i, " " }``

produces:

``5 6 7 8 9 10``

Yields:

• (i)

Returns:

 ``` ``` ```# File 'numeric.c' /* * call-seq: * int.upto(limit) {|i| block } => int * * Iterates block, passing in integer values from int * up to and including limit. * * 5.upto(10) { |i| print i, " " } * * produces: * * 5 6 7 8 9 10 */ static VALUE int_upto(from, to) VALUE from, to; { RETURN_ENUMERATOR(from, 1, &to); if (FIXNUM_P(from) && FIXNUM_P(to)) { long i, end; end = FIX2LONG(to); for (i = FIX2LONG(from); i <= end; i++) { rb_yield(LONG2FIX(i)); } } else { VALUE i = from, c; while (!(c = rb_funcall(i, '>', 1, to))) { rb_yield(i); i = rb_funcall(i, '+', 1, INT2FIX(1)); } if (NIL_P(c)) rb_cmperr(i, to); } return from; }```