themenabend rubyc3d2.de/media/ta-ruby.pdf · ruby has simple syntax, partially inspired by eiffel...

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

Upload: others

Post on 06-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Themenabend Ruby

Sven & Astro

March 28, 2006

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

Page 2: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Hilfe?

www.ruby-lang.org

”Programming Ruby“

http://www.rubycentral.com/book

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

Page 10: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

irb – Interactive Ruby

Um schnell etwas auszuprobieren

Toller bc(1)-Ersatz

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

Page 12: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

irb – Interactive Ruby

Um schnell etwas auszuprobieren

Toller bc(1)-Ersatz

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

Page 13: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

ri

Integrierte Dokumentation

Lasst sich mit Rdoc erstellen

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

Page 14: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

ri

Integrierte Dokumentation

Lasst sich mit Rdoc erstellen

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

Page 15: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Primitive Datentypen

nil.class=> NilClass

false.class=> FalseClass

true.class=> TrueClass

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

Page 46: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Primitive Datentypen

nil.class=> NilClass

false.class=> FalseClass

true.class=> TrueClass

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

Page 47: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Primitive Datentypen

nil.class=> NilClass

false.class=> FalseClass

true.class=> TrueClass

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

Page 48: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Alles hat ein Ergebnis

variable = (if bedingungthen wert

elseelse wert

end)

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

Page 102: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Bibliotheken laden

require ’thread’

$: wird durchsucht

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

Page 108: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Bibliotheken laden

require ’thread’

$: wird durchsucht

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

Page 109: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Klassen

class Drinkdef initialize(name)@name = name

enddef drinkputs "Drinking #{@name}"

endend

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

Page 138: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

”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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

”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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

”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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

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 Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Factory method

def factory methodConcreteProduct.new

end

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

Page 191: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Observer

require ’observer’

module Observable als Mixin

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

Page 192: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Singleton

require ’observer’

module Singleton als Mixin

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

Page 193: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Decorator

Object#extend

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

Page 194: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Upcoming...

3 Ruby-MediaWikiMediaWiki?Ruby-MediaWiki!

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

Page 195: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

MediaWiki?

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

Page 196: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

MediaWiki?

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

Page 197: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

MediaWiki?

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

Page 198: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Upcoming...

3 Ruby-MediaWikiMediaWiki?Ruby-MediaWiki!

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

Page 199: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Ruby-MediaWiki!

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

Page 200: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Ruby-MediaWiki!

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

Page 201: Themenabend Rubyc3d2.de/media/ta-ruby.pdf · Ruby has simple syntax, partially inspired by Eiffel and Ada. Ruby has exception handling features, like Java or Python, to make it easy

Ruby-MediaWiki!

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