Programmierung in Ruby

Der Leitfaden der Pragmatischen Programmierer

class Integer
Parent: Numeric
Version: 1.6

Index:

chr downto integer? next step succ times upto



Subclasses: Bignum, Fixnum

Integer ist die Basis für die beiden konkreten Klassen, die ganze Zahlen enthalten, Bignum und Fixnum.

instance methods
chr int.chr -> aString

Liefert einen String, der das durch diese Zahl repräsentierte ASCII-Zeichen enthält.

65.chr » "A"
?a.chr » "a"
230.chr » "\346"

downto int.downto( anInteger ) {| i | block }

-> int

Iteriert den block und übergibt die absteigenden Werte von int runter bis inklusive anInteger.

5.downto(1) { |n| print n, ".. " }
print "  Liftoff!\n"
erzeugt:
5.. 4.. 3.. 2.. 1..   Liftoff!

integer? int.integer? -> true

Liefert immer true.

next int.next -> anInteger

Liefert den Integer, der gleich ist mit int + 1.

1.next » 2
(-1).next » 0

step int.step( endNum, step ) {| i | block }

-> int

Ruft den block auf mit der Folge von Zahlen beginnend bei int und dann jedes Mal um step erhöht. Die Schleife endet, wenn der zu übergebende Wert größer als endNum ist (bei positivem step) oder kleiner als endNum (bei negativem step).

1.step(10, 2) { |i| print i, " " }
erzeugt:
1 3 5 7 9

succ int.succ -> anInteger

Synonym für Integer#next.

times int.times {| i | block }

-> int

Iteriert den Block int Mal, übergibt dabei Werte von 0 bis int - 1.

5.times do |i|
  print i, " "
end
print "\n"
erzeugt:
0 1 2 3 4

upto int.upto( anInteger ) {| i | block }

-> int

Iteriert den block und übergibt Integer-Werte von int hinauf bis inklusive anInteger.

5.upto(10) { |i| print i, " " }
erzeugt:
5 6 7 8 9 10


Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Übersetzung: Jürgen Katins
Für das englische Original:
© 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0. That reference is available for download.
Diese Lizenz sowie das Original vom Herbst 2001 bilden die Grundlage der Übersetzung
Es wird darauf hingewiesen, dass sich die Lizenz des englischen Originals inzwischen geändert hat.
Für die deutsche Übersetzung:
© 2002 Jürgen Katins
Der Copyright-Eigner stellt folgende Lizenzen zur Verfügung:
Nicht-freie Lizenz:
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.
Freie Lizenz:
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".