Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be...

201
Themenabend Ruby Sven & Astro March 28, 2006 Sven & Astro () Themenabend Ruby March 28, 2006 1 / 90

Transcript of Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be...

Page 1: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Themenabend Ruby

Sven & Astro

March 28, 2006

Sven & Astro () Themenabend Ruby March 28, 2006 1 / 90

Page 2: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Ruby has simple syntax, partially inspired by Eiffel and Ada.

Ruby has exception handling features, like Java or Python, to make it easy to handle errors.

Ruby’s operators are syntax sugar for the methods. You can redefine them easily.

Ruby is a complete, full, pure object oriented language: OOL. This means all data in Ruby is an object, in the sense ofSmalltalk: no exceptions. Example: In Ruby, the number 1 is an instance of class Fixnum.

Ruby’s OO is carefully designed to be both complete and open for improvements. Example: Ruby has the ability to addmethods to a class, or even to an instance during runtime. So, if needed, an instance of one class *can* behavedifferently from other instances of the same class.

Ruby features single inheritance only, *on purpose*. But Ruby knows the concept of modules (called Categories inObjective-C). Modules are collections of methods. Every class can import a module and so gets all its methods for free.Some of us think that this is a much clearer way than multiple inheritance, which is complex, and not used very oftencompared with single inheritance (don’t count C++ here, as it has often no other choice due to strong type checking!).

Ruby features true closures. Not just unnamed function, but with present variable bindings.

Ruby features blocks in its syntax (code surrounded by ’{’ ... ’}’ or ’do’ ... ’end’). These blocks can be passed tomethods, or converted into closures.

Ruby features a true mark-and-sweep garbage collector. It works with all Ruby objects. You don’t have to care aboutmaintaining reference counts in extension libraries. This is better for your health. ;-)

Writing C extensions in Ruby is easier than in Perl or Python, due partly to the garbage collector, and partly to the fineextension API. SWIG interface is also available.

Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers(instances of class Fixnum) and large integers (Bignum), but you need not worry over which one is used currently. If avalue is small enough, an integer is a Fixnum, otherwise it is a Bignum. Conversion occurs automatically.

Ruby needs no variable declarations. It uses simple naming conventions to denote the scope of variables. Examples:simple ’var’ = local variable, ’@var’ = instance variable, ’$var’ = global variable. So it is also not necessary to use atiresome ’self.’ prepended to every instance member.

Ruby can load extension libraries dynamically if an OS allows.

Ruby features OS independent threading. Thus, for all platforms on which Ruby runs, you also have multithreading,regardless of if the OS supports it or not, even on MS-DOS! ;-)

Ruby is highly portable: it is developed mostly on Linux, but works on many types of UNIX, DOS, Windows95/98/Me/NT/2000/XP, MacOS, BeOS, OS/2, etc.

Sven & Astro () Themenabend Ruby March 28, 2006 2 / 90

Page 3: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 3 / 90

Page 4: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Herkunft

”Perl“: Perle

”Ruby“: Rubin

”Ruby ist eine objektorientierte, interpretierte Programmiersprache.

Sie hat ihre Wurzeln in Perl, Smalltalk, Python, LISP, Bash undCLU.“ Wikipedia1

1http://de.wikipedia.org/wiki/Ruby %28Programmiersprache%29Sven & Astro () Themenabend Ruby March 28, 2006 4 / 90

Page 5: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Entwicklung

Februar 1993: Yukihiro”Matz“ Matsumoto beginnt Entwicklung

Dezember 1995: Erste Veroffentlichung

$ ruby -vruby 1.8.4 (2005-12-24) [i386-freebsd6]

Ruby 1.9: Rite, Ruby2

Sven & Astro () Themenabend Ruby March 28, 2006 5 / 90

Page 6: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Entwicklung

Februar 1993: Yukihiro”Matz“ Matsumoto beginnt Entwicklung

Dezember 1995: Erste Veroffentlichung

$ ruby -vruby 1.8.4 (2005-12-24) [i386-freebsd6]

Ruby 1.9: Rite, Ruby2

Sven & Astro () Themenabend Ruby March 28, 2006 5 / 90

Page 7: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Entwicklung

Februar 1993: Yukihiro”Matz“ Matsumoto beginnt Entwicklung

Dezember 1995: Erste Veroffentlichung

$ ruby -vruby 1.8.4 (2005-12-24) [i386-freebsd6]

Ruby 1.9: Rite, Ruby2

Sven & Astro () Themenabend Ruby March 28, 2006 5 / 90

Page 8: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 6 / 90

Page 9: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hilfe?

www.ruby-lang.org

”Programming Ruby“

http://www.rubycentral.com/book

Sven & Astro () Themenabend Ruby March 28, 2006 7 / 90

Page 10: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 8 / 90

Page 11: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

irb – Interactive Ruby

Um schnell etwas auszuprobieren

Toller bc(1)-Ersatz

Sven & Astro () Themenabend Ruby March 28, 2006 9 / 90

Page 12: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

irb – Interactive Ruby

Um schnell etwas auszuprobieren

Toller bc(1)-Ersatz

Sven & Astro () Themenabend Ruby March 28, 2006 9 / 90

Page 13: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

ri

Integrierte Dokumentation

Lasst sich mit Rdoc erstellen

Sven & Astro () Themenabend Ruby March 28, 2006 10 / 90

Page 14: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

ri

Integrierte Dokumentation

Lasst sich mit Rdoc erstellen

Sven & Astro () Themenabend Ruby March 28, 2006 10 / 90

Page 15: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 11 / 90

Page 16: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Variablen

Lokale Variablen: v = 23

Instanzvariablen: @v = 23

Klassenvariablen: @@v = 23

Globale Variablen: $v = 23Zum Beispiel: $stdin, $stdout, $stderr

Sven & Astro () Themenabend Ruby March 28, 2006 12 / 90

Page 17: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Variablen

Lokale Variablen: v = 23

Instanzvariablen: @v = 23

Klassenvariablen: @@v = 23

Globale Variablen: $v = 23Zum Beispiel: $stdin, $stdout, $stderr

Sven & Astro () Themenabend Ruby March 28, 2006 12 / 90

Page 18: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Variablen

Lokale Variablen: v = 23

Instanzvariablen: @v = 23

Klassenvariablen: @@v = 23

Globale Variablen: $v = 23Zum Beispiel: $stdin, $stdout, $stderr

Sven & Astro () Themenabend Ruby March 28, 2006 12 / 90

Page 19: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Variablen

Lokale Variablen: v = 23

Instanzvariablen: @v = 23

Klassenvariablen: @@v = 23

Globale Variablen: $v = 23Zum Beispiel: $stdin, $stdout, $stderr

Sven & Astro () Themenabend Ruby March 28, 2006 12 / 90

Page 20: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 13 / 90

Page 21: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles ist ein Objekt

mein string = "Hallo"=> "Hallo"

mein string.reverse=> "ollaH"

"Hallo".reverse=> "ollaH"

mein string => "Hallo"

mein string.reverse! => "ollaH"

mein string => "ollaH"

Sven & Astro () Themenabend Ruby March 28, 2006 14 / 90

Page 22: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles ist ein Objekt

mein string = "Hallo"=> "Hallo"

mein string.reverse=> "ollaH"

"Hallo".reverse=> "ollaH"

mein string => "Hallo"

mein string.reverse! => "ollaH"

mein string => "ollaH"

Sven & Astro () Themenabend Ruby March 28, 2006 14 / 90

Page 23: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles ist ein Objekt

mein string = "Hallo"=> "Hallo"

mein string.reverse=> "ollaH"

"Hallo".reverse=> "ollaH"

mein string => "Hallo"

mein string.reverse! => "ollaH"

mein string => "ollaH"

Sven & Astro () Themenabend Ruby March 28, 2006 14 / 90

Page 24: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles ist ein Objekt

mein string = "Hallo"=> "Hallo"

mein string.reverse=> "ollaH"

"Hallo".reverse=> "ollaH"

mein string => "Hallo"

mein string.reverse! => "ollaH"

mein string => "ollaH"

Sven & Astro () Themenabend Ruby March 28, 2006 14 / 90

Page 25: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles ist ein Objekt

mein string = "Hallo"=> "Hallo"

mein string.reverse=> "ollaH"

"Hallo".reverse=> "ollaH"

mein string => "Hallo"

mein string.reverse! => "ollaH"

mein string => "ollaH"

Sven & Astro () Themenabend Ruby March 28, 2006 14 / 90

Page 26: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 15 / 90

Page 27: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Konventionen

Module, Klassen, Konstanten, Klassenmethoden, Modulmethoden:Net::HTTP, Thread::stop ⇔ Thread.stop

Instanzmethoden:String#upcase, Array#collect

Modul- und Klassennamen in CamelCase

Sven & Astro () Themenabend Ruby March 28, 2006 16 / 90

Page 28: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Konventionen

Module, Klassen, Konstanten, Klassenmethoden, Modulmethoden:Net::HTTP, Thread::stop ⇔ Thread.stop

Instanzmethoden:String#upcase, Array#collect

Modul- und Klassennamen in CamelCase

Sven & Astro () Themenabend Ruby March 28, 2006 16 / 90

Page 29: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Konventionen

Module, Klassen, Konstanten, Klassenmethoden, Modulmethoden:Net::HTTP, Thread::stop ⇔ Thread.stop

Instanzmethoden:String#upcase, Array#collect

Modul- und Klassennamen in CamelCase

Sven & Astro () Themenabend Ruby March 28, 2006 16 / 90

Page 30: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Konventionen (Methodennamen)

Methodennamen kleingeschrieben: Array#each with index

Funktion mit booleschem Ergebnis:heuhaufen.include? nadelIO#closed?

Gefahrliche Funktionen: String#reverse!, Array#collect!

Sven & Astro () Themenabend Ruby March 28, 2006 17 / 90

Page 31: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Konventionen (Methodennamen)

Methodennamen kleingeschrieben: Array#each with index

Funktion mit booleschem Ergebnis:heuhaufen.include? nadelIO#closed?

Gefahrliche Funktionen: String#reverse!, Array#collect!

Sven & Astro () Themenabend Ruby March 28, 2006 17 / 90

Page 32: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Konventionen (Methodennamen)

Methodennamen kleingeschrieben: Array#each with index

Funktion mit booleschem Ergebnis:heuhaufen.include? nadelIO#closed?

Gefahrliche Funktionen: String#reverse!, Array#collect!

Sven & Astro () Themenabend Ruby March 28, 2006 17 / 90

Page 33: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 18 / 90

Page 34: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

p

zeilenverzeichnis = "Zeile 1\nZeile 2"

p zeilenverzeichnis"Zeile 1\nZeile 2"

puts zeilenverzeichnis.inspect"Zeile 1\nZeile 2"

p zeilenverzeichnis.inspect.classString

p zeilenverzeichnis.inspect"\"Zeile 1\\nZeile 2\""

Sven & Astro () Themenabend Ruby March 28, 2006 19 / 90

Page 35: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

p

zeilenverzeichnis = "Zeile 1\nZeile 2"

p zeilenverzeichnis"Zeile 1\nZeile 2"

puts zeilenverzeichnis.inspect"Zeile 1\nZeile 2"

p zeilenverzeichnis.inspect.classString

p zeilenverzeichnis.inspect"\"Zeile 1\\nZeile 2\""

Sven & Astro () Themenabend Ruby March 28, 2006 19 / 90

Page 36: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

p

zeilenverzeichnis = "Zeile 1\nZeile 2"

p zeilenverzeichnis"Zeile 1\nZeile 2"

puts zeilenverzeichnis.inspect"Zeile 1\nZeile 2"

p zeilenverzeichnis.inspect.classString

p zeilenverzeichnis.inspect"\"Zeile 1\\nZeile 2\""

Sven & Astro () Themenabend Ruby March 28, 2006 19 / 90

Page 37: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

p

zeilenverzeichnis = "Zeile 1\nZeile 2"

p zeilenverzeichnis"Zeile 1\nZeile 2"

puts zeilenverzeichnis.inspect"Zeile 1\nZeile 2"

p zeilenverzeichnis.inspect.classString

p zeilenverzeichnis.inspect"\"Zeile 1\\nZeile 2\""

Sven & Astro () Themenabend Ruby March 28, 2006 19 / 90

Page 38: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

p

zeilenverzeichnis = "Zeile 1\nZeile 2"

p zeilenverzeichnis"Zeile 1\nZeile 2"

puts zeilenverzeichnis.inspect"Zeile 1\nZeile 2"

p zeilenverzeichnis.inspect.classString

p zeilenverzeichnis.inspect"\"Zeile 1\\nZeile 2\""

Sven & Astro () Themenabend Ruby March 28, 2006 19 / 90

Page 39: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

p

a = [11, ’X’]

p a[11, "X"]

p meine instanz#<MeineKlasse:0x82690b4 @variable2="discordia",@variable1="eris">

Sven & Astro () Themenabend Ruby March 28, 2006 20 / 90

Page 40: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

p

a = [11, ’X’]

p a[11, "X"]

p meine instanz#<MeineKlasse:0x82690b4 @variable2="discordia",@variable1="eris">

Sven & Astro () Themenabend Ruby March 28, 2006 20 / 90

Page 41: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

p

a = [11, ’X’]

p a[11, "X"]

p meine instanz#<MeineKlasse:0x82690b4 @variable2="discordia",@variable1="eris">

Sven & Astro () Themenabend Ruby March 28, 2006 20 / 90

Page 42: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

inspect selbst definieren

class << meine instanzdef inspect"<#self.class(#@variable1,#@variable2)>"

endend

p meine instanz<MeineKlasse(eris,discordia)>

Sven & Astro () Themenabend Ruby March 28, 2006 21 / 90

Page 43: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

inspect selbst definieren

class << meine instanzdef inspect"<#self.class(#@variable1,#@variable2)>"

endend

p meine instanz<MeineKlasse(eris,discordia)>

Sven & Astro () Themenabend Ruby March 28, 2006 21 / 90

Page 44: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 22 / 90

Page 45: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Primitive Datentypen

nil.class=> NilClass

false.class=> FalseClass

true.class=> TrueClass

Sven & Astro () Themenabend Ruby March 28, 2006 23 / 90

Page 46: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Primitive Datentypen

nil.class=> NilClass

false.class=> FalseClass

true.class=> TrueClass

Sven & Astro () Themenabend Ruby March 28, 2006 23 / 90

Page 47: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Primitive Datentypen

nil.class=> NilClass

false.class=> FalseClass

true.class=> TrueClass

Sven & Astro () Themenabend Ruby March 28, 2006 23 / 90

Page 48: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Strings

’Hier mussen nur \\ und \’ escaped werden.’

"Hier konnen wir auch anderes escapen\n""und Code einbetten: #{ENV[’PATH’]}"

Sven & Astro () Themenabend Ruby March 28, 2006 24 / 90

Page 49: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Strings

’Hier mussen nur \\ und \’ escaped werden.’

"Hier konnen wir auch anderes escapen\n"

"und Code einbetten: #{ENV[’PATH’]}"

Sven & Astro () Themenabend Ruby March 28, 2006 24 / 90

Page 50: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Strings

’Hier mussen nur \\ und \’ escaped werden.’

"Hier konnen wir auch anderes escapen\n""und Code einbetten: #{ENV[’PATH’]}"

Sven & Astro () Themenabend Ruby March 28, 2006 24 / 90

Page 51: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Nummern

23.class=> Fixnum

23.5.class=> Float

(5**100).to s.size=> 32(5**100).class=> Bignum

Sven & Astro () Themenabend Ruby March 28, 2006 25 / 90

Page 52: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Nummern

23.class=> Fixnum

23.5.class=> Float

(5**100).to s.size=> 32(5**100).class=> Bignum

Sven & Astro () Themenabend Ruby March 28, 2006 25 / 90

Page 53: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Nummern

23.class=> Fixnum

23.5.class=> Float

(5**100).to s.size=> 32(5**100).class=> Bignum

Sven & Astro () Themenabend Ruby March 28, 2006 25 / 90

Page 54: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays

mein array = [5, 23, ’R00by’]

mein array=> [5, 23, ’R00by’]

mein array[1]=> 23

mein array.reverse=> [’R00by’, 23, 5]

Sven & Astro () Themenabend Ruby March 28, 2006 26 / 90

Page 55: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays

mein array = [5, 23, ’R00by’]

mein array=> [5, 23, ’R00by’]

mein array[1]=> 23

mein array.reverse=> [’R00by’, 23, 5]

Sven & Astro () Themenabend Ruby March 28, 2006 26 / 90

Page 56: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays

mein array = [5, 23, ’R00by’]

mein array=> [5, 23, ’R00by’]

mein array[1]=> 23

mein array.reverse=> [’R00by’, 23, 5]

Sven & Astro () Themenabend Ruby March 28, 2006 26 / 90

Page 57: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays

mein array.each do |element|puts element

end

mein array.each { |element|puts element

}523R00bymein array.each with index { |element,index|puts "#{index}: #{element}"

}0: 51: 232: R00by=> [5, 23, "R00by"]

Sven & Astro () Themenabend Ruby March 28, 2006 27 / 90

Page 58: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays

mein array.each do |element|puts element

endmein array.each { |element|puts element

}

523R00bymein array.each with index { |element,index|puts "#{index}: #{element}"

}0: 51: 232: R00by=> [5, 23, "R00by"]

Sven & Astro () Themenabend Ruby March 28, 2006 27 / 90

Page 59: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays

mein array.each do |element|puts element

endmein array.each { |element|puts element

}523R00by

mein array.each with index { |element,index|puts "#{index}: #{element}"

}0: 51: 232: R00by=> [5, 23, "R00by"]

Sven & Astro () Themenabend Ruby March 28, 2006 27 / 90

Page 60: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays

mein array.each do |element|puts element

endmein array.each { |element|puts element

}523R00bymein array.each with index { |element,index|puts "#{index}: #{element}"

}

0: 51: 232: R00by=> [5, 23, "R00by"]

Sven & Astro () Themenabend Ruby March 28, 2006 27 / 90

Page 61: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays

mein array.each do |element|puts element

endmein array.each { |element|puts element

}523R00bymein array.each with index { |element,index|puts "#{index}: #{element}"

}0: 51: 232: R00by=> [5, 23, "R00by"]

Sven & Astro () Themenabend Ruby March 28, 2006 27 / 90

Page 62: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays und Variablenzuweisung

a = ’Hund’, ’Katze’=> ["Hund", "Katze"]

hund, katze = a=> ["Hund", "Katze"]

hund=> "Hund"

katze=> "Katze"

hund, katze = ’Hund’, ’Katze’=> ["Hund", "Katze"]

hund=> "Hund"

katze=> "Katze"

Inhalt tauschen: a, b = b, a

Sven & Astro () Themenabend Ruby March 28, 2006 28 / 90

Page 63: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays und Variablenzuweisung

a = ’Hund’, ’Katze’=> ["Hund", "Katze"]

hund, katze = a=> ["Hund", "Katze"]

hund=> "Hund"

katze=> "Katze"

hund, katze = ’Hund’, ’Katze’=> ["Hund", "Katze"]

hund=> "Hund"

katze=> "Katze"

Inhalt tauschen: a, b = b, a

Sven & Astro () Themenabend Ruby March 28, 2006 28 / 90

Page 64: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays und Variablenzuweisung

a = ’Hund’, ’Katze’=> ["Hund", "Katze"]

hund, katze = a=> ["Hund", "Katze"]

hund=> "Hund"

katze=> "Katze"

hund, katze = ’Hund’, ’Katze’=> ["Hund", "Katze"]

hund=> "Hund"

katze=> "Katze"

Inhalt tauschen: a, b = b, a

Sven & Astro () Themenabend Ruby March 28, 2006 28 / 90

Page 65: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Arrays und Variablenzuweisung

a = ’Hund’, ’Katze’=> ["Hund", "Katze"]

hund, katze = a=> ["Hund", "Katze"]

hund=> "Hund"

katze=> "Katze"

hund, katze = ’Hund’, ’Katze’=> ["Hund", "Katze"]

hund=> "Hund"

katze=> "Katze"

Inhalt tauschen: a, b = b, a

Sven & Astro () Themenabend Ruby March 28, 2006 28 / 90

Page 66: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hashes

mein hash = {’Mate’ => 1.0, ’Datenschleuder’ => 2.5}mein hash=> {’Datenschleuder’ => 2.5, ’Mate’ => 1.0}

mein hash[’Mate’]=> 1.0

mein hash.keys=> [’Datenschleuder’, ’Mate’]

Sven & Astro () Themenabend Ruby March 28, 2006 29 / 90

Page 67: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hashes

mein hash = {’Mate’ => 1.0, ’Datenschleuder’ => 2.5}mein hash=> {’Datenschleuder’ => 2.5, ’Mate’ => 1.0}mein hash[’Mate’]=> 1.0

mein hash.keys=> [’Datenschleuder’, ’Mate’]

Sven & Astro () Themenabend Ruby March 28, 2006 29 / 90

Page 68: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hashes

mein hash = {’Mate’ => 1.0, ’Datenschleuder’ => 2.5}mein hash=> {’Datenschleuder’ => 2.5, ’Mate’ => 1.0}mein hash[’Mate’]=> 1.0

mein hash.keys=> [’Datenschleuder’, ’Mate’]

Sven & Astro () Themenabend Ruby March 28, 2006 29 / 90

Page 69: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hashes (Iteratoren)

mein hash.each do |artikel,preis|puts "#{artikel} kostet #{preis} FRZ"

end

Datenschleuder kostet 2.5 FRZMate kostet 1.0 FRZ=> {"Datenschleuder"=>2.5, "Mate"=>1.0}puts mein hash.collect { |artikel,preis| "#{artikel}:#{preis}" }.join("\n")Datenschleuder: 2.5Mate: 1.0=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 30 / 90

Page 70: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hashes (Iteratoren)

mein hash.each do |artikel,preis|puts "#{artikel} kostet #{preis} FRZ"

end

Datenschleuder kostet 2.5 FRZMate kostet 1.0 FRZ=> {"Datenschleuder"=>2.5, "Mate"=>1.0}

puts mein hash.collect { |artikel,preis| "#{artikel}:#{preis}" }.join("\n")Datenschleuder: 2.5Mate: 1.0=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 30 / 90

Page 71: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hashes (Iteratoren)

mein hash.each do |artikel,preis|puts "#{artikel} kostet #{preis} FRZ"

end

Datenschleuder kostet 2.5 FRZMate kostet 1.0 FRZ=> {"Datenschleuder"=>2.5, "Mate"=>1.0}puts mein hash.collect { |artikel,preis| "#{artikel}:#{preis}" }.join("\n")

Datenschleuder: 2.5Mate: 1.0=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 30 / 90

Page 72: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hashes (Iteratoren)

mein hash.each do |artikel,preis|puts "#{artikel} kostet #{preis} FRZ"

end

Datenschleuder kostet 2.5 FRZMate kostet 1.0 FRZ=> {"Datenschleuder"=>2.5, "Mate"=>1.0}puts mein hash.collect { |artikel,preis| "#{artikel}:#{preis}" }.join("\n")Datenschleuder: 2.5Mate: 1.0=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 30 / 90

Page 73: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Ranges

mein range = 5..23=> 5..23

mein range.include? 8=> true

mein range.include? 1=> false

case coolnesswhen 0..20 then puts "Perl"when 21..60 then puts "Python"when 61..100 then puts "Ruby"

end

Sven & Astro () Themenabend Ruby March 28, 2006 31 / 90

Page 74: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Ranges

mein range = 5..23=> 5..23

mein range.include? 8=> true

mein range.include? 1=> false

case coolnesswhen 0..20 then puts "Perl"when 21..60 then puts "Python"when 61..100 then puts "Ruby"

end

Sven & Astro () Themenabend Ruby March 28, 2006 31 / 90

Page 75: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Ranges

mein range = 5..23=> 5..23

mein range.include? 8=> true

mein range.include? 1=> false

case coolnesswhen 0..20 then puts "Perl"when 21..60 then puts "Python"when 61..100 then puts "Ruby"

end

Sven & Astro () Themenabend Ruby March 28, 2006 31 / 90

Page 76: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Regexp

puts "Mentions Ruby" if text =~ /Ruby/

if ’Ruby is cool’ =~ /(.+) is (.+)/puts "#{$1} ist #{$2}"

end

Ruby ist cool

if m = r.match(’Ruby is cool’)p m.captures

end

["Ruby", "cool"]

’Ruby is cool. Sky is blue.’.scan(/ *(.+?) is(.+?)\./)=> [["Ruby", "cool"], ["Sky", "blue"]]

Sven & Astro () Themenabend Ruby March 28, 2006 32 / 90

Page 77: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Regexp

puts "Mentions Ruby" if text =~ /Ruby/

if ’Ruby is cool’ =~ /(.+) is (.+)/puts "#{$1} ist #{$2}"

end

Ruby ist cool

if m = r.match(’Ruby is cool’)p m.captures

end

["Ruby", "cool"]

’Ruby is cool. Sky is blue.’.scan(/ *(.+?) is(.+?)\./)=> [["Ruby", "cool"], ["Sky", "blue"]]

Sven & Astro () Themenabend Ruby March 28, 2006 32 / 90

Page 78: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Regexp

puts "Mentions Ruby" if text =~ /Ruby/

if ’Ruby is cool’ =~ /(.+) is (.+)/puts "#{$1} ist #{$2}"

end

Ruby ist cool

if m = r.match(’Ruby is cool’)p m.captures

end

["Ruby", "cool"]

’Ruby is cool. Sky is blue.’.scan(/ *(.+?) is(.+?)\./)=> [["Ruby", "cool"], ["Sky", "blue"]]

Sven & Astro () Themenabend Ruby March 28, 2006 32 / 90

Page 79: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Regexp

puts "Mentions Ruby" if text =~ /Ruby/

if ’Ruby is cool’ =~ /(.+) is (.+)/puts "#{$1} ist #{$2}"

end

Ruby ist cool

if m = r.match(’Ruby is cool’)p m.captures

end

["Ruby", "cool"]

’Ruby is cool. Sky is blue.’.scan(/ *(.+?) is(.+?)\./)=> [["Ruby", "cool"], ["Sky", "blue"]]

Sven & Astro () Themenabend Ruby March 28, 2006 32 / 90

Page 80: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Symbols

Symbole reprasentieren Namen oder Strings

a = "Ruby"b = "Ruby"a.object id=> 403028500

b.object id=> 403010020

c = :Rubyd = :"Ruby"c.object id=> 4121870

d.object id=> 4121870

Sven & Astro () Themenabend Ruby March 28, 2006 33 / 90

Page 81: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Symbols

Symbole reprasentieren Namen oder Strings

a = "Ruby"b = "Ruby"a.object id=> 403028500

b.object id=> 403010020

c = :Rubyd = :"Ruby"c.object id=> 4121870

d.object id=> 4121870

Sven & Astro () Themenabend Ruby March 28, 2006 33 / 90

Page 82: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Symbols

Symbole reprasentieren Namen oder Strings

a = "Ruby"b = "Ruby"a.object id=> 403028500

b.object id=> 403010020

c = :Rubyd = :"Ruby"c.object id=> 4121870

d.object id=> 4121870

Sven & Astro () Themenabend Ruby March 28, 2006 33 / 90

Page 83: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 34 / 90

Page 84: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

if

if Ruby::is cool?applause!

end

if hacker.is hacking?hacker.continue hacking

elsehacker.start hacking

end

unless hacker.is hacking?hacker.start hacking

elsehacker.continue hacking

end

Alternativ: (condition ? then clause : else clause)

Sven & Astro () Themenabend Ruby March 28, 2006 35 / 90

Page 85: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

if

if Ruby::is cool?applause!

end

if hacker.is hacking?hacker.continue hacking

elsehacker.start hacking

end

unless hacker.is hacking?hacker.start hacking

elsehacker.continue hacking

end

Alternativ: (condition ? then clause : else clause)

Sven & Astro () Themenabend Ruby March 28, 2006 35 / 90

Page 86: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

if

if Ruby::is cool?applause!

end

if hacker.is hacking?hacker.continue hacking

elsehacker.start hacking

end

unless hacker.is hacking?hacker.start hacking

elsehacker.continue hacking

end

Alternativ: (condition ? then clause : else clause)

Sven & Astro () Themenabend Ruby March 28, 2006 35 / 90

Page 87: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

if

if Ruby::is cool?applause!

end

if hacker.is hacking?hacker.continue hacking

elsehacker.start hacking

end

unless hacker.is hacking?hacker.start hacking

elsehacker.continue hacking

end

Alternativ: (condition ? then clause : else clause)

Sven & Astro () Themenabend Ruby March 28, 2006 35 / 90

Page 88: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

while

i = 0=> 0

while i < 5puts (i += 1)

end

12345=> nil

Abbruch: break

Sven & Astro () Themenabend Ruby March 28, 2006 36 / 90

Page 89: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

while

i = 0=> 0

while i < 5puts (i += 1)

end

12345=> nil

Abbruch: break

Sven & Astro () Themenabend Ruby March 28, 2006 36 / 90

Page 90: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

while

i = 0=> 0

while i < 5puts (i += 1)

end

12345=> nil

Abbruch: break

Sven & Astro () Themenabend Ruby March 28, 2006 36 / 90

Page 91: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

while

i = 0=> 0

while i < 5puts (i += 1)

end

12345=> nil

Abbruch: break

Sven & Astro () Themenabend Ruby March 28, 2006 36 / 90

Page 92: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hintendran

exit if user.wants to exit?

exit unless user.wants to go on?

i = 0=> 0

beginputs(i+=1)

end while i < 5

12345=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 37 / 90

Page 93: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hintendran

exit if user.wants to exit?

exit unless user.wants to go on?

i = 0=> 0

beginputs(i+=1)

end while i < 5

12345=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 37 / 90

Page 94: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hintendran

exit if user.wants to exit?

exit unless user.wants to go on?

i = 0=> 0

beginputs(i+=1)

end while i < 5

12345=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 37 / 90

Page 95: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Hintendran

exit if user.wants to exit?

exit unless user.wants to go on?

i = 0=> 0

beginputs(i+=1)

end while i < 5

12345=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 37 / 90

Page 96: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

case

case inputLinewhen "debug"dumpDebugInfodumpSymbols

when /p\s+(\w+)/dumpVariable($1)

when "quit", "exit"exit

elseprint "Illegal command: #{inputLine}"

end

Sven & Astro () Themenabend Ruby March 28, 2006 38 / 90

Page 97: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 39 / 90

Page 98: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Der ternare Operator

Bekannt, der ternare Operator:bedingung ? then wert : else wert

4.times { |zahl|puts "#{zahl} " +(zahl == 2 ? ’ist’ : ’ist nicht’) +" Zwei."

}0 ist nicht Zwei.1 ist nicht Zwei.2 ist Zwei.3 ist nicht Zwei.

Sven & Astro () Themenabend Ruby March 28, 2006 40 / 90

Page 99: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Der ternare Operator

Bekannt, der ternare Operator:bedingung ? then wert : else wert

4.times { |zahl|puts "#{zahl} " +(zahl == 2 ? ’ist’ : ’ist nicht’) +" Zwei."

}

0 ist nicht Zwei.1 ist nicht Zwei.2 ist Zwei.3 ist nicht Zwei.

Sven & Astro () Themenabend Ruby March 28, 2006 40 / 90

Page 100: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Der ternare Operator

Bekannt, der ternare Operator:bedingung ? then wert : else wert

4.times { |zahl|puts "#{zahl} " +(zahl == 2 ? ’ist’ : ’ist nicht’) +" Zwei."

}0 ist nicht Zwei.1 ist nicht Zwei.2 ist Zwei.3 ist nicht Zwei.

Sven & Astro () Themenabend Ruby March 28, 2006 40 / 90

Page 101: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles hat ein Ergebnis

variable = (if bedingungthen wert

elseelse wert

end)

Sven & Astro () Themenabend Ruby March 28, 2006 41 / 90

Page 102: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles hat ein Ergebnis

puts "Zufall " + case randwhen 0..0.3 then "klein"when 0.3..0.7 then "mittel"when 0.7..1 then "gross"else "kaputt"

end

Zufall gross

Zufall klein

Zufall mittel

Sven & Astro () Themenabend Ruby March 28, 2006 42 / 90

Page 103: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles hat ein Ergebnis

puts "Zufall " + case randwhen 0..0.3 then "klein"when 0.3..0.7 then "mittel"when 0.7..1 then "gross"else "kaputt"

end

Zufall gross

Zufall klein

Zufall mittel

Sven & Astro () Themenabend Ruby March 28, 2006 42 / 90

Page 104: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles hat ein Ergebnis

puts "Zufall " + case randwhen 0..0.3 then "klein"when 0.3..0.7 then "mittel"when 0.7..1 then "gross"else "kaputt"

end

Zufall gross

Zufall klein

Zufall mittel

Sven & Astro () Themenabend Ruby March 28, 2006 42 / 90

Page 105: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Alles hat ein Ergebnis

puts "Zufall " + case randwhen 0..0.3 then "klein"when 0.3..0.7 then "mittel"when 0.7..1 then "gross"else "kaputt"

end

Zufall gross

Zufall klein

Zufall mittel

Sven & Astro () Themenabend Ruby March 28, 2006 42 / 90

Page 106: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 43 / 90

Page 107: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Bibliotheken laden

require ’thread’

$: wird durchsucht

Sven & Astro () Themenabend Ruby March 28, 2006 44 / 90

Page 108: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Bibliotheken laden

require ’thread’

$: wird durchsucht

Sven & Astro () Themenabend Ruby March 28, 2006 44 / 90

Page 109: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 45 / 90

Page 110: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Methoden

Haben immer einen Ruckgabewert!

def hallo weltputs "Hallo Welt!"

end

hallo welt()Hallo Welt!=> nil

hallo weltHallo Welt!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 46 / 90

Page 111: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Methoden

Haben immer einen Ruckgabewert!

def hallo weltputs "Hallo Welt!"

end

hallo welt()Hallo Welt!=> nil

hallo weltHallo Welt!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 46 / 90

Page 112: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Methoden

Haben immer einen Ruckgabewert!

def hallo weltputs "Hallo Welt!"

end

hallo welt()Hallo Welt!=> nil

hallo weltHallo Welt!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 46 / 90

Page 113: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Parameter

def hallo(whom, bangs)puts "Hallo #{whom}#{’!’*bangs}"

end

hallo ’Peter’, 5Hallo Peter!!!!!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 47 / 90

Page 114: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Parameter

def hallo(whom, bangs)puts "Hallo #{whom}#{’!’*bangs}"

end

hallo ’Peter’, 5Hallo Peter!!!!!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 47 / 90

Page 115: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Default-Parameter

Normaler Parameter darf keinem Default-Parameter folgen!

def hallo(whom=’Eris’)puts "Hallo #{whom}!"

end

hallo ’Peter’Hallo Peter!=> nil

halloHallo Eris!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 48 / 90

Page 116: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Default-Parameter

Normaler Parameter darf keinem Default-Parameter folgen!

def hallo(whom=’Eris’)puts "Hallo #{whom}!"

end

hallo ’Peter’Hallo Peter!=> nil

halloHallo Eris!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 48 / 90

Page 117: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Default-Parameter

Normaler Parameter darf keinem Default-Parameter folgen!

def hallo(whom=’Eris’)puts "Hallo #{whom}!"

end

hallo ’Peter’Hallo Peter!=> nil

halloHallo Eris!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 48 / 90

Page 118: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Variable Parameterliste

def debug parameters(*a)p a

end

debug parameters(23, 5, 42)[23, 5, 42]

Aha, ein Array!

Sven & Astro () Themenabend Ruby March 28, 2006 49 / 90

Page 119: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Variable Parameterliste

def debug parameters(*a)p a

end

debug parameters(23, 5, 42)[23, 5, 42]

Aha, ein Array!

Sven & Astro () Themenabend Ruby March 28, 2006 49 / 90

Page 120: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Parameter als Hash

def hash me(hsh)p hsh

end

hash me ’Datenschleuder’=>2.50, ’Aufkleber’=>1.00{"Aufkleber"=>1.0, "Datenschleuder"=>2.5}hash me(’Datenschleuder’=>2.50, ’Aufkleber’=>1.00){"Aufkleber"=>1.0, "Datenschleuder"=>2.5}

Sven & Astro () Themenabend Ruby March 28, 2006 50 / 90

Page 121: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Parameter als Hash

def hash me(hsh)p hsh

end

hash me ’Datenschleuder’=>2.50, ’Aufkleber’=>1.00{"Aufkleber"=>1.0, "Datenschleuder"=>2.5}hash me(’Datenschleuder’=>2.50, ’Aufkleber’=>1.00){"Aufkleber"=>1.0, "Datenschleuder"=>2.5}

Sven & Astro () Themenabend Ruby March 28, 2006 50 / 90

Page 122: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 51 / 90

Page 123: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke

Benutzung: mein array.each { |e| puts e }

def tuyield

end

tu

LocalJumpError: no block givenfrom (irb):2:in ‘tu’from (irb):4from :0

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 52 / 90

Page 124: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke

Benutzung: mein array.each { |e| puts e }def tuyield

end

tu

LocalJumpError: no block givenfrom (irb):2:in ‘tu’from (irb):4from :0

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 52 / 90

Page 125: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke

Benutzung: mein array.each { |e| puts e }def tuyield

end

tu

LocalJumpError: no block givenfrom (irb):2:in ‘tu’from (irb):4from :0

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 52 / 90

Page 126: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke

Benutzung: mein array.each { |e| puts e }def tuyield

end

tu

LocalJumpError: no block givenfrom (irb):2:in ‘tu’from (irb):4from :0

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 52 / 90

Page 127: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke implizit

def tuyield if block given?

end

tu=> nil

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 53 / 90

Page 128: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke implizit

def tuyield if block given?

end

tu=> nil

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 53 / 90

Page 129: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke implizit

def tuyield if block given?

end

tu=> nil

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 53 / 90

Page 130: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke explizit

def tu(&block)block.call if block

end

tu=> nil

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 54 / 90

Page 131: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke explizit

def tu(&block)block.call if block

end

tu=> nil

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 54 / 90

Page 132: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke explizit

def tu(&block)block.call if block

end

tu=> nil

tu { puts ’Hallo’ }Hallo=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 54 / 90

Page 133: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke merken

most important function = lambda { puts "Hello World!" }

most important function.call

Hello World!=> nil

tu { most important function.call }Hello World!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 55 / 90

Page 134: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke merken

most important function = lambda { puts "Hello World!" }most important function.call

Hello World!=> nil

tu { most important function.call }Hello World!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 55 / 90

Page 135: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Blocke merken

most important function = lambda { puts "Hello World!" }most important function.call

Hello World!=> nil

tu { most important function.call }Hello World!=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 55 / 90

Page 136: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 56 / 90

Page 137: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Klassen

class Drinkdef initialize(name)@name = name

enddef drinkputs "Drinking #{@name}"

endend

Sven & Astro () Themenabend Ruby March 28, 2006 57 / 90

Page 138: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Vererben

class Mate < Drinkdef initializesuper ’Club Mate’

endend

m = Mate.new=> #<Mate:0x8152664 @name="Club Mate">

m.drinkDrinking Club Mate=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 58 / 90

Page 139: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Vererben

class Mate < Drinkdef initializesuper ’Club Mate’

endend

m = Mate.new=> #<Mate:0x8152664 @name="Club Mate">

m.drinkDrinking Club Mate=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 58 / 90

Page 140: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Vererben

class Mate < Drinkdef initializesuper ’Club Mate’

endend

m = Mate.new=> #<Mate:0x8152664 @name="Club Mate">

m.drinkDrinking Club Mate=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 58 / 90

Page 141: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter

class Drinkattr reader :name

end

m.name=> "Club Mate"

m.name = ’B33r’NoMethodError: undefined method ‘name=’ for#<Mate:0x8152664 @name="Club Mate">from (irb):38from :0

Sven & Astro () Themenabend Ruby March 28, 2006 59 / 90

Page 142: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter

class Drinkattr reader :name

end

m.name=> "Club Mate"

m.name = ’B33r’NoMethodError: undefined method ‘name=’ for#<Mate:0x8152664 @name="Club Mate">from (irb):38from :0

Sven & Astro () Themenabend Ruby March 28, 2006 59 / 90

Page 143: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter

class Drinkattr reader :name

end

m.name=> "Club Mate"

m.name = ’B33r’NoMethodError: undefined method ‘name=’ for#<Mate:0x8152664 @name="Club Mate">from (irb):38from :0

Sven & Astro () Themenabend Ruby March 28, 2006 59 / 90

Page 144: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter & Setter

class Drinkattr accessor :name

end

m.name=> "Club Mate"

m.name = ’Jolt’=> "Jolt"

m.name=> "Jolt"

Sven & Astro () Themenabend Ruby March 28, 2006 60 / 90

Page 145: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter & Setter

class Drinkattr accessor :name

end

m.name=> "Club Mate"

m.name = ’Jolt’=> "Jolt"

m.name=> "Jolt"

Sven & Astro () Themenabend Ruby March 28, 2006 60 / 90

Page 146: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter & Setter

class Drinkattr accessor :name

end

m.name=> "Club Mate"

m.name = ’Jolt’=> "Jolt"

m.name=> "Jolt"

Sven & Astro () Themenabend Ruby March 28, 2006 60 / 90

Page 147: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter & Setter

class Drinkattr accessor :name

end

m.name=> "Club Mate"

m.name = ’Jolt’=> "Jolt"

m.name=> "Jolt"

Sven & Astro () Themenabend Ruby March 28, 2006 60 / 90

Page 148: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter und Setter selbst bauen

class Drinkdef initialize(erster name)self.name = erster name

enddef name@name

enddef name=(neuer name)if neuer name == ’Beer’raise ’Please stay sober while coding’

else@name = neuer name

endend

end

Sven & Astro () Themenabend Ruby March 28, 2006 61 / 90

Page 149: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter und Setter selbst bauen

m = Drink.new(’Mate’)=> #<Drink:0x812cef0 @name="Mate">

m.name = ’Milk’=> "Milk"

m.name = ’Beer’RuntimeError: Please stay sober while codingfrom (irb):7:in ‘name=’from (irb):14from :0

b = Drink.new(’Beer’)RuntimeError: Please stay sober while codingfrom (irb):7:in ‘name=’from (irb):17:in ‘initialize’from (irb):20from :0

Sven & Astro () Themenabend Ruby March 28, 2006 62 / 90

Page 150: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter und Setter selbst bauen

m = Drink.new(’Mate’)=> #<Drink:0x812cef0 @name="Mate">

m.name = ’Milk’=> "Milk"

m.name = ’Beer’RuntimeError: Please stay sober while codingfrom (irb):7:in ‘name=’from (irb):14from :0

b = Drink.new(’Beer’)RuntimeError: Please stay sober while codingfrom (irb):7:in ‘name=’from (irb):17:in ‘initialize’from (irb):20from :0

Sven & Astro () Themenabend Ruby March 28, 2006 62 / 90

Page 151: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter und Setter selbst bauen

m = Drink.new(’Mate’)=> #<Drink:0x812cef0 @name="Mate">

m.name = ’Milk’=> "Milk"

m.name = ’Beer’RuntimeError: Please stay sober while codingfrom (irb):7:in ‘name=’from (irb):14from :0

b = Drink.new(’Beer’)RuntimeError: Please stay sober while codingfrom (irb):7:in ‘name=’from (irb):17:in ‘initialize’from (irb):20from :0

Sven & Astro () Themenabend Ruby March 28, 2006 62 / 90

Page 152: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Klassen erweitern

Dringend benotigte Funktionen

Flicken von fehlerhaftem Fremdcode

Verstreuen der Implementation auf mehrere Dateien

Beispielproblem von turbo24prg: String#to b

class Stringdef to bself == ’true’

endend

’chunky bacon’.to b=> false

’true’.to b=> true

Sven & Astro () Themenabend Ruby March 28, 2006 63 / 90

Page 153: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Klassen erweitern

Dringend benotigte Funktionen

Flicken von fehlerhaftem Fremdcode

Verstreuen der Implementation auf mehrere Dateien

Beispielproblem von turbo24prg: String#to b

class Stringdef to bself == ’true’

endend

’chunky bacon’.to b=> false

’true’.to b=> true

Sven & Astro () Themenabend Ruby March 28, 2006 63 / 90

Page 154: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Klassen erweitern

Dringend benotigte Funktionen

Flicken von fehlerhaftem Fremdcode

Verstreuen der Implementation auf mehrere Dateien

Beispielproblem von turbo24prg: String#to b

class Stringdef to bself == ’true’

endend

’chunky bacon’.to b=> false

’true’.to b=> true

Sven & Astro () Themenabend Ruby March 28, 2006 63 / 90

Page 155: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Klassen erweitern

Dringend benotigte Funktionen

Flicken von fehlerhaftem Fremdcode

Verstreuen der Implementation auf mehrere Dateien

Beispielproblem von turbo24prg: String#to b

class Stringdef to bself == ’true’

endend

’chunky bacon’.to b=> false

’true’.to b=> true

Sven & Astro () Themenabend Ruby March 28, 2006 63 / 90

Page 156: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Object#method missing

class MeineKlassedef method missing(*a)p a

endend

k = MeineKlasse.new=> #<MeineKlasse:0x813db38>

k.hello[:hello]=> nil

k.hello(23, 42)[:hello, 23, 42]=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 64 / 90

Page 157: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Object#method missing

class MeineKlassedef method missing(*a)p a

endend

k = MeineKlasse.new=> #<MeineKlasse:0x813db38>

k.hello[:hello]=> nil

k.hello(23, 42)[:hello, 23, 42]=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 64 / 90

Page 158: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Object#method missing

class MeineKlassedef method missing(*a)p a

endend

k = MeineKlasse.new=> #<MeineKlasse:0x813db38>

k.hello[:hello]=> nil

k.hello(23, 42)[:hello, 23, 42]=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 64 / 90

Page 159: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Object#method missing

class MeineKlassedef method missing(*a)p a

endend

k = MeineKlasse.new=> #<MeineKlasse:0x813db38>

k.hello[:hello]=> nil

k.hello(23, 42)[:hello, 23, 42]=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 64 / 90

Page 160: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

”Builder”

class Builderdef self.printputs yield(new)

enddef method missing(fname, attrs={})"<#{fname}" + attrs.collect { |k,v| " #{k}=\"#{v}\""

}.to s + (block given? ? ">#{yield}</#{fname}>" :’/>’)end

end

Sven & Astro () Themenabend Ruby March 28, 2006 65 / 90

Page 161: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

”Builder” angewandt

Builder.print { |b|b.html {b.head {b.title {’Hello World!’

}} +b.body {b.input ’name’=>’search’

}}

}

<html><head><title>HelloWorld!</title></head><body><inputname="search"/></body></html>

Sven & Astro () Themenabend Ruby March 28, 2006 66 / 90

Page 162: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

”Builder” angewandt

Builder.print { |b|b.html {b.head {b.title {’Hello World!’

}} +b.body {b.input ’name’=>’search’

}}

}<html><head><title>HelloWorld!</title></head><body><inputname="search"/></body></html>

Sven & Astro () Themenabend Ruby March 28, 2006 66 / 90

Page 163: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter definieren mit Module#define method

class MeineKlassedef initialize@variable = ’hallo’

enddefine method(:var) do@variable

endend

m = MeineKlasse.new=> #<MeineKlasse:0x8060bf4 @variable="hallo">

m.var=> "hallo"

Sven & Astro () Themenabend Ruby March 28, 2006 67 / 90

Page 164: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter definieren mit Module#define method

class MeineKlassedef initialize@variable = ’hallo’

enddefine method(:var) do@variable

endend

m = MeineKlasse.new=> #<MeineKlasse:0x8060bf4 @variable="hallo">

m.var=> "hallo"

Sven & Astro () Themenabend Ruby March 28, 2006 67 / 90

Page 165: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Getter definieren mit Module#define method

class MeineKlassedef initialize@variable = ’hallo’

enddefine method(:var) do@variable

endend

m = MeineKlasse.new=> #<MeineKlasse:0x8060bf4 @variable="hallo">

m.var=> "hallo"

Sven & Astro () Themenabend Ruby March 28, 2006 67 / 90

Page 166: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 68 / 90

Page 167: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Module und Namensaume

module Jabberclass Clientdef initialize# Code

endend

end

client = Jabber::Client.new

Sven & Astro () Themenabend Ruby March 28, 2006 69 / 90

Page 168: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Module und Namensaume

module Jabberclass Clientdef initialize# Code

endend

end

client = Jabber::Client.new

Sven & Astro () Themenabend Ruby March 28, 2006 69 / 90

Page 169: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Module und Mixins

module Eatabledef eatputs "#{self.class} #{@name} has been eaten."

endend

class Fryhstyckinclude Eatabledef initialize@name = ’Egg and Ham’

endend

f = Fryhstyck.new=> #<Fryhstyck:0x811e378 @name="Egg and Ham">

f.eatFryhstyck Egg and Ham has been eaten.=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 70 / 90

Page 170: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Module und Mixins

module Eatabledef eatputs "#{self.class} #{@name} has been eaten."

endend

class Fryhstyckinclude Eatabledef initialize@name = ’Egg and Ham’

endend

f = Fryhstyck.new=> #<Fryhstyck:0x811e378 @name="Egg and Ham">

f.eatFryhstyck Egg and Ham has been eaten.=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 70 / 90

Page 171: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Module und Mixins

module Eatabledef eatputs "#{self.class} #{@name} has been eaten."

endend

class Fryhstyckinclude Eatabledef initialize@name = ’Egg and Ham’

endend

f = Fryhstyck.new=> #<Fryhstyck:0x811e378 @name="Egg and Ham">

f.eatFryhstyck Egg and Ham has been eaten.=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 70 / 90

Page 172: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Module und Mixins

module Eatabledef eatputs "#{self.class} #{@name} has been eaten."

endend

class Fryhstyckinclude Eatabledef initialize@name = ’Egg and Ham’

endend

f = Fryhstyck.new=> #<Fryhstyck:0x811e378 @name="Egg and Ham">

f.eatFryhstyck Egg and Ham has been eaten.=> nil

Sven & Astro () Themenabend Ruby March 28, 2006 70 / 90

Page 173: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Modulmethoden

module MeinModuldef MeinModul.homelessputs "I’ve got no instance"

endend

MeinModul.homelessI’ve got no instance

Sven & Astro () Themenabend Ruby March 28, 2006 71 / 90

Page 174: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Modulmethoden

module MeinModuldef MeinModul.homelessputs "I’ve got no instance"

endend

MeinModul.homelessI’ve got no instance

Sven & Astro () Themenabend Ruby March 28, 2006 71 / 90

Page 175: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Klassenmethoden

class MeineKlassedef self.neunew

endend

MeineKlasse.neu=> #<MeineKlasse:0x810d3fc>

class MeineKindklasse < MeineKlasseend

MeineKindklasse.neu=> #<MeineKindklasse:0x814e500>

Sven & Astro () Themenabend Ruby March 28, 2006 72 / 90

Page 176: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Klassenmethoden

class MeineKlassedef self.neunew

endend

MeineKlasse.neu=> #<MeineKlasse:0x810d3fc>

class MeineKindklasse < MeineKlasseend

MeineKindklasse.neu=> #<MeineKindklasse:0x814e500>

Sven & Astro () Themenabend Ruby March 28, 2006 72 / 90

Page 177: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Klassenmethoden

class MeineKlassedef self.neunew

endend

MeineKlasse.neu=> #<MeineKlasse:0x810d3fc>

class MeineKindklasse < MeineKlasseend

MeineKindklasse.neu=> #<MeineKindklasse:0x814e500>

Sven & Astro () Themenabend Ruby March 28, 2006 72 / 90

Page 178: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Klassenmethoden

class MeineKlassedef self.neunew

endend

MeineKlasse.neu=> #<MeineKlasse:0x810d3fc>

class MeineKindklasse < MeineKlasseend

MeineKindklasse.neu=> #<MeineKindklasse:0x814e500>

Sven & Astro () Themenabend Ruby March 28, 2006 72 / 90

Page 179: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 73 / 90

Page 180: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Exceptions

beginf = File.new(’/nonexistant/imaginary file’)

rescueputs "Fehler: #{$!}"

end

Fehler: No such file or directory -/nonexistant/imaginary file

Sven & Astro () Themenabend Ruby March 28, 2006 74 / 90

Page 181: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Exceptions

beginf = File.new(’/nonexistant/imaginary file’)

rescueputs "Fehler: #{$!}"

end

Fehler: No such file or directory -/nonexistant/imaginary file

Sven & Astro () Themenabend Ruby March 28, 2006 74 / 90

Page 182: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Exceptions: Begrenzen

beginf = File.new(’/nonexistant/imaginary file’)

rescue Errno => ep e

end

Errno::ENOENT: No such file or directory -/nonexistant/imaginary filefrom (irb):2:in ‘initialize’from (irb):2from :0

Sven & Astro () Themenabend Ruby March 28, 2006 75 / 90

Page 183: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Exceptions: Begrenzen

beginf = File.new(’/nonexistant/imaginary file’)

rescue Errno => ep e

end

Errno::ENOENT: No such file or directory -/nonexistant/imaginary filefrom (irb):2:in ‘initialize’from (irb):2from :0

Sven & Astro () Themenabend Ruby March 28, 2006 75 / 90

Page 184: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Exceptions: Syntaxfehler

beginxxx

rescue Exception => eputs "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"

end

NameError: undefined local variable or method ‘xxx’for main:Object(irb):7:in ‘irb binding’/usr/local/lib/ruby/1.8/irb/workspace.rb:52:in‘irb binding’:0

Sven & Astro () Themenabend Ruby March 28, 2006 76 / 90

Page 185: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Exceptions: Syntaxfehler

beginxxx

rescue Exception => eputs "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"

end

NameError: undefined local variable or method ‘xxx’for main:Object(irb):7:in ‘irb binding’/usr/local/lib/ruby/1.8/irb/workspace.rb:52:in‘irb binding’:0

Sven & Astro () Themenabend Ruby March 28, 2006 76 / 90

Page 186: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Exceptions: Mehr

begin# Ausnahmenwerfender Code

ensure# Was auf jeden Fall gemacht werden muss

end

raise "Computer gone. No processor to run on left."

raise NotImplementedError.new(’Programmer too lazy’)

Nochmal ausfuhren: retry

Sven & Astro () Themenabend Ruby March 28, 2006 77 / 90

Page 187: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Exceptions: Mehr

begin# Ausnahmenwerfender Code

ensure# Was auf jeden Fall gemacht werden muss

end

raise "Computer gone. No processor to run on left."

raise NotImplementedError.new(’Programmer too lazy’)

Nochmal ausfuhren: retry

Sven & Astro () Themenabend Ruby March 28, 2006 77 / 90

Page 188: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Exceptions: Mehr

begin# Ausnahmenwerfender Code

ensure# Was auf jeden Fall gemacht werden muss

end

raise "Computer gone. No processor to run on left."

raise NotImplementedError.new(’Programmer too lazy’)

Nochmal ausfuhren: retry

Sven & Astro () Themenabend Ruby March 28, 2006 77 / 90

Page 189: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

2 EinfuhrungMetaHilfe?irb & riVariablenAlles ist ein ObjektKonventionenprint-DebuggingPrimitive DatentypenKontrollstrukturenAlles hat ein ErgebnisBibliotheken ladenMethodenBlockeKlassenModuleExceptionsDesign Patterns

Sven & Astro () Themenabend Ruby March 28, 2006 78 / 90

Page 190: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Factory method

def factory methodConcreteProduct.new

end

Sven & Astro () Themenabend Ruby March 28, 2006 79 / 90

Page 191: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Observer

require ’observer’

module Observable als Mixin

Sven & Astro () Themenabend Ruby March 28, 2006 80 / 90

Page 192: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Singleton

require ’observer’

module Singleton als Mixin

Sven & Astro () Themenabend Ruby March 28, 2006 81 / 90

Page 193: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Decorator

Object#extend

Sven & Astro () Themenabend Ruby March 28, 2006 82 / 90

Page 194: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

3 Ruby-MediaWikiMediaWiki?Ruby-MediaWiki!

Sven & Astro () Themenabend Ruby March 28, 2006 83 / 90

Page 195: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

MediaWiki?

Sven & Astro () Themenabend Ruby March 28, 2006 84 / 90

Page 196: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

MediaWiki?

Sven & Astro () Themenabend Ruby March 28, 2006 85 / 90

Page 197: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

MediaWiki?

Sven & Astro () Themenabend Ruby March 28, 2006 86 / 90

Page 198: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Upcoming...

3 Ruby-MediaWikiMediaWiki?Ruby-MediaWiki!

Sven & Astro () Themenabend Ruby March 28, 2006 87 / 90

Page 199: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Ruby-MediaWiki!

Sven & Astro () Themenabend Ruby March 28, 2006 88 / 90

Page 200: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Ruby-MediaWiki!

Sven & Astro () Themenabend Ruby March 28, 2006 89 / 90

Page 201: Themenabend Ruby - c3d2 · SWIG interface is also available. Integers in Ruby can (and should) be used without counting their internal representation. There *are* small integers (instances

Ruby-MediaWiki!

Sven & Astro () Themenabend Ruby March 28, 2006 90 / 90