Programming Ruby
The Pragmatic Programmer's Guide
class Integer
Parent:
Numeric
Version:
1.6
Index:
chr
downto
integer?
next
step
succ
times
upto
Subclasses: Bignum, Fixnum
Integer is the basis for the two concrete classes that hold
whole numbers,
Bignum and
Fixnum.
instance methods
Returns a string containing the ASCII character represented by
the receiver's value.
65.chr
サ
"A"
?a.chr
サ
"a"
230.chr
サ
"346円"
downto
int.downto(
anInteger ) {| i | block }
->
int
Iterates
block, passing decreasing values
from
int down to and including
anInteger.
5.downto(1) { |n| print n, ".. " }
print " Liftoff!\n"
produces:
5.. 4.. 3.. 2.. 1.. Liftoff!
Always returns
true.
next
int.next ->
anInteger
Returns the
Integer equal to
int + 1.
1.next
サ
2
(-1).next
サ
0
step
int.step(
endNum,
step ) {| i | block }
->
int
Invokes
block with the sequence of numbers starting at
int,
incremented by
step on each call. The loop
finishes when the value to be passed to the block is greater
than
endNum (if
step is positive) or less than
endNum (if
step is negative).
1.step(10, 2) { |i| print i, " " }
produces:
succ
int.succ ->
anInteger
Synonym for
Integer#next
.
times
int.times {| i | block }
->
int
Iterates block
int times, passing in values from zero to
int - 1.
5.times do |i|
print i, " "
end
print "\n"
produces:
upto
int.upto(
anInteger ) {| i | block }
->
int
Iterates
block, passing in integer values from
int up to and including
anInteger.
5.upto(10) { |i| print i, " " }
produces:
Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"
Copyright
©
2001 by Addison Wesley Longman, Inc. This material may
be distributed only subject to the terms and conditions set forth in
the Open Publication License, v1.0 or later (the latest version is
presently available at
http://www.opencontent.org/openpub/)).
Distribution of substantively modified versions of this document is
prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard
(paper) book form is prohibited unless prior permission is obtained
from the copyright holder.