SlideShare una empresa de Scribd logo
1 de 17
Descargar para leer sin conexión
Ruby::Allgemeines     Ruby::Exception Handling   Abschluss




                         Ruby

                    Alexander Boldt


                    11. Januar 2008




Alexander Boldt
Ruby
Ruby::Allgemeines                   Ruby::Exception Handling       Abschluss




 Einstieg



       Yukihiro Matsumoto (Matz), creator of Ruby:
                 I wanted to minimize my frustration during
              programming, so I want to minimize my effort in
              programming. That was my primary goal in designing
              Ruby. I want to have fun in programming myself.




Alexander Boldt
Ruby
Ruby::Allgemeines                  Ruby::Exception Handling   Abschluss




 Inhaltsverzeichnis


       1   Ruby::Allgemeines
             Ruby::Allgemeines::Unterschiede
             Ruby::Allgemeines::Syntax
             Ruby::Allgemeines::Flow Control

       2   Ruby::Exception Handling
             Ruby::Exception Handling::Verarbeitung
             Ruby::Exception Handling::Sprachunterst¨tzung
                                                    u

       3   Abschluss



Alexander Boldt
Ruby
Ruby::Allgemeines                  Ruby::Exception Handling              Abschluss



Ruby::Allgemeines::Unterschiede

                                  ¨
 Ruby::Allgemeines::Unterschiede::Uberblick




       Abbildung: Unterschiede zu anderen Programmiersprachen aus Beginning
       Ruby (Apress)



Alexander Boldt
Ruby
Ruby::Allgemeines                            Ruby::Exception Handling   Abschluss



Ruby::Allgemeines::Unterschiede

 Ruby::Allgemeines::Unterschiede::Dynamische Typisierung


              Java:     int i = 0;

              C++:       int i = 0;

              Ruby:

                                  Listing 1: Dynamische Typisierung
           1 i = 2
           2 p u t s i . c l a s s #Fixnum
           3 i = i ∗∗ 100
           4 p u t s i . c l a s s #Bignum




Alexander Boldt
Ruby
Ruby::Allgemeines                      Ruby::Exception Handling          Abschluss



Ruby::Allgemeines::Unterschiede

 Ruby::Unterschiede::Weitere Unterschiede


              Klassen¨nderung jederzeit m¨glich
                     a                   o
              Keine Typendeklaration
              Konstruktor heißt initialize
              Keine Kompilierung notwendig
              Instanzvariablen nur Zugriff uber Methoden
                                          ¨
              Bl¨cke ohne Klammern - Schl¨sselw¨rter (z.B. do und end)
                o                           u    o
              YAML mehr als XML verwendet
              etc.



Alexander Boldt
Ruby
Ruby::Allgemeines                    Ruby::Exception Handling    Abschluss



Ruby::Allgemeines::Syntax

 Ruby::Allgemeines::Syntax::Deklarationen




              Klassen: class Name ... end
              Methoden: def name ... end
              Variablen: @@name, @name, $name, Name, name, ...




Alexander Boldt
Ruby
Ruby::Allgemeines                                        Ruby::Exception Handling                         Abschluss



Ruby::Allgemeines::Syntax

 Ruby::Allgemeines::Syntax::Vererbung


                                                Listing 2: Vererbung
   1 c l a s s Song
   2     d e f i n i t i a l i z e ( name , a r t i s t , d u r a t i o n )
   3         @name             = name
   4         @artist           = artist
   5         @duration = duration
   6     end
   7 end
   8
   9 c l a s s KaraokeSong < Song
  10     d e f i n i t i a l i z e ( name , a r t i s t , d u r a t i o n , l y r i c s )
  11         s u p e r ( name , a r t i s t , d u r a t i o n )
  12         @lyrics = lyrics
  13     end
  14 end
  15
  16 k a r a o k e = KaraokeSong . new ( ” T e s t ” , ” I c h ” , 2 0 , ” So g e h t d a s L i e d ” )




Alexander Boldt
Ruby
Ruby::Allgemeines                                     Ruby::Exception Handling   Abschluss



Ruby::Allgemeines::Syntax

 Ruby::Allgemeines::Syntax::Arrays, Hashes



                                      Listing 3: Array und Hashes
   1   ## A r r a y D e k l a r a t i o n e n
   2   a r r a y = [ ” Wert1 ” , 2 , ” w e r t 3” ]
   3   # = [ ” Wert1 ” , ” 2 ” , ” w e r t 3 ” ]
            >
   4   a r r a y 2 = %w( Wert1 2 w e r t  3 )
   5   # = [ ” Wert1 ” , ” 2 ” , ” w e r t 3 ” ]
            >
   6
   7   ## Hash D e k l a r a t i o n
   8   c o l o r s = { ” red ”       = 0 xf00 ,
                                      >
   9                   ” green ” = 0 x0f0 ,
                                      >
  10                   ” blue ” = 0 x00f
                                      >
  11                 }
  12   #= {” g r e e n ”=>240, ” b l u e ”=>15, ” r e d ”=>3840}
          >




Alexander Boldt
Ruby
Ruby::Allgemeines                                                 Ruby::Exception Handling   Abschluss



Ruby::Allgemeines::Syntax

 Ruby::Allgemeines::Syntax::Regul¨re Ausdr¨cke
                                 a        u




                                              Listing 4: Regul¨re Ausdr¨cke
                                                              a        u
    1   ## R e g u l a e r e A u s d r u e c k e
    2   ” t h i s i s a t e s t ” . sub ( / [ a e i o u ] / ,   ’∗ ’)
    3   # = th∗s i s a t e s t
             >
    4
    5   ” t h i s i s a t e s t ” . s p l i t (/ s / )
    6   # = [ ’ this ’ , ’ is ’ , ’a ’ , ’ test ’ ]
             >




Alexander Boldt
Ruby
Ruby::Allgemeines                                        Ruby::Exception Handling        Abschluss



Ruby::Allgemeines::Flow Control

 Ruby::Allgemeines::Flow Control::If und Unless - elsif -
 case .. when

                                              Listing 5: Flow Control
   1   # i f und u n l e s s , e l s e , c a s e . . when
   2   a g e = 10
   3   p u t s ” S i e s i n d zu j u n g f u e r d a s System ” i f age < 18
   4   p u t s ” S i e s i n d zu j u n g f u e r d a s System ” u n l e s s age >= 18
   5
   6   i f a g e < 18
   7     p u t s ” S i e s i n d zu j u n g f u e r d a s System ”
   8      e l s i f a g e == 18
   9       p u t s ” H a r t an d e r G r e n z e ”
  10     else
  11       p u t s ” S i e haben v o l l e n Z u g r i f f ”
  12   end
  13
  14   c a s e age
  15     when 1 . . 1 7
  16       p u t s ” S i e s i n d zu j u n g f u e r d a s System ”
  17     when 18
  18       p u t s ” H a r t an d e r G r e n z e ”
  19     else
  20       p u t s ” S i e haben v o l l e n Z u g r i f f ”
  21   end

Alexander Boldt
Ruby
Ruby::Allgemeines                                             Ruby::Exception Handling   Abschluss



Ruby::Allgemeines::Flow Control

 Ruby::Allgemeines::Flow Control::upto, each, while, until


                                      Listing 6: upto - each - while - until
   1   ## upto , each , w h i l e u n t i l . . .
   2   1 . u p t o ( 5 ) { | number | p u t s number ∗2 }
   3
   4   x = [1 ,2 ,3 ,4]
   5   x . each { | item | puts item}
   6
   7   x . e a c h do | i t e m |
   8     puts ” A k t u e l l e r I n h a l t   i s t #{i t e m }”
   9   end
  10
  11   x = 1
  12   w h i l e x < 100 # − u n t i l x > 99
                            >
  13      puts x
  14      x = x ∗ 2
  15   end




Alexander Boldt
Ruby
Ruby::Allgemeines                        Ruby::Exception Handling     Abschluss



Ruby::Exception Handling::Verarbeitung

                                         ¨
 Ruby::Exception Handling::Verarbeitung::Uberblick



       Die Oberklasse Exception besitzt uber 30 vordefinierte Subclassen
                                        ¨
           ZeroDevisionError
              NoMethodError
              RuntimeError
              ArgumentError
              etc.




Alexander Boldt
Ruby
Ruby::Allgemeines                        Ruby::Exception Handling      Abschluss



Ruby::Exception Handling::Verarbeitung

 Ruby::Exception Handling::Verarbeitung::Ruby’s Weg



              Automatisch z.B. bei inkorrektem Aufruf von Funktionen
              Selbst definiert im Code (raise)
              Suche nach Handler f¨r jeweilige Exception (resque
                                  u
              NameDerException), wenn gefunden Ausf¨hrung, sonst
                                                       u
              Hochreichen (resque Exception) oder sterben mit Error
              Meldung




Alexander Boldt
Ruby
Ruby::Allgemeines                                            Ruby::Exception Handling             Abschluss



Ruby::Exception Handling::Sprachunterst¨tzung
                                       u

 Ruby::Exception Handling::Sprachunterst¨tzung::Eigene
                                        u
 Exceptions


                           Listing 7: Raise - Resque - Eigene Exceptions
   1   # Eigene Exceptions
   2   c l a s s BadDataException < RuntimeError
   3   end
   4
   5   c l a s s Person
   6       d e f i n i t i a l i z e ( name )
   7           r a i s e B a d D a t a E x c e p t i o n , ” K e i n e Name” i f name . empty ?
   8       end
   9   end
  10
  11   begin
  12    p = P e r s o n . new ( ” ” ) # − E r r o r
                                            >
  13    r e s c u e BadDataException
  14    p = P e r s o n . new ( ” D e f a u l t ” )
  15   end




Alexander Boldt
Ruby
Ruby::Allgemeines                                         Ruby::Exception Handling                      Abschluss



Ruby::Exception Handling::Sprachunterst¨tzung
                                       u

 Ruby::Exception Handling::Sprachunterst¨tzung::Catch
                                        u
 and Throw



                                           Listing 8: Catch and Throw
    1 # Ca tc h und Throw
    2 c a t c h ( : f i n i s h ) do
    3     1 0 0 0 . t i m e s do
    4         x = rand (1000)
    5         puts x
    6         t h r o w : f i n i s h i f x == 123
    7     end
    8     p u t s ” G e n e r a t e d 1000 random numbers w i t h o u t g e n e r a t i n g 1 2 3 ! ”
    9 end




Alexander Boldt
Ruby
Ruby::Allgemeines                Ruby::Exception Handling            Abschluss




 Abschluss::Fragen?!




       Fragen?!
       Danke f¨r Eure Aufmerksamkeit. Bei Fragen stehe ich nun zur
              u
       Verf¨gung
           u




Alexander Boldt
Ruby

Más contenido relacionado

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Ruby

  • 1. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby Alexander Boldt 11. Januar 2008 Alexander Boldt Ruby
  • 2. Ruby::Allgemeines Ruby::Exception Handling Abschluss Einstieg Yukihiro Matsumoto (Matz), creator of Ruby: I wanted to minimize my frustration during programming, so I want to minimize my effort in programming. That was my primary goal in designing Ruby. I want to have fun in programming myself. Alexander Boldt Ruby
  • 3. Ruby::Allgemeines Ruby::Exception Handling Abschluss Inhaltsverzeichnis 1 Ruby::Allgemeines Ruby::Allgemeines::Unterschiede Ruby::Allgemeines::Syntax Ruby::Allgemeines::Flow Control 2 Ruby::Exception Handling Ruby::Exception Handling::Verarbeitung Ruby::Exception Handling::Sprachunterst¨tzung u 3 Abschluss Alexander Boldt Ruby
  • 4. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Allgemeines::Unterschiede ¨ Ruby::Allgemeines::Unterschiede::Uberblick Abbildung: Unterschiede zu anderen Programmiersprachen aus Beginning Ruby (Apress) Alexander Boldt Ruby
  • 5. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Allgemeines::Unterschiede Ruby::Allgemeines::Unterschiede::Dynamische Typisierung Java: int i = 0; C++: int i = 0; Ruby: Listing 1: Dynamische Typisierung 1 i = 2 2 p u t s i . c l a s s #Fixnum 3 i = i ∗∗ 100 4 p u t s i . c l a s s #Bignum Alexander Boldt Ruby
  • 6. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Allgemeines::Unterschiede Ruby::Unterschiede::Weitere Unterschiede Klassen¨nderung jederzeit m¨glich a o Keine Typendeklaration Konstruktor heißt initialize Keine Kompilierung notwendig Instanzvariablen nur Zugriff uber Methoden ¨ Bl¨cke ohne Klammern - Schl¨sselw¨rter (z.B. do und end) o u o YAML mehr als XML verwendet etc. Alexander Boldt Ruby
  • 7. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Allgemeines::Syntax Ruby::Allgemeines::Syntax::Deklarationen Klassen: class Name ... end Methoden: def name ... end Variablen: @@name, @name, $name, Name, name, ... Alexander Boldt Ruby
  • 8. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Allgemeines::Syntax Ruby::Allgemeines::Syntax::Vererbung Listing 2: Vererbung 1 c l a s s Song 2 d e f i n i t i a l i z e ( name , a r t i s t , d u r a t i o n ) 3 @name = name 4 @artist = artist 5 @duration = duration 6 end 7 end 8 9 c l a s s KaraokeSong < Song 10 d e f i n i t i a l i z e ( name , a r t i s t , d u r a t i o n , l y r i c s ) 11 s u p e r ( name , a r t i s t , d u r a t i o n ) 12 @lyrics = lyrics 13 end 14 end 15 16 k a r a o k e = KaraokeSong . new ( ” T e s t ” , ” I c h ” , 2 0 , ” So g e h t d a s L i e d ” ) Alexander Boldt Ruby
  • 9. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Allgemeines::Syntax Ruby::Allgemeines::Syntax::Arrays, Hashes Listing 3: Array und Hashes 1 ## A r r a y D e k l a r a t i o n e n 2 a r r a y = [ ” Wert1 ” , 2 , ” w e r t 3” ] 3 # = [ ” Wert1 ” , ” 2 ” , ” w e r t 3 ” ] > 4 a r r a y 2 = %w( Wert1 2 w e r t 3 ) 5 # = [ ” Wert1 ” , ” 2 ” , ” w e r t 3 ” ] > 6 7 ## Hash D e k l a r a t i o n 8 c o l o r s = { ” red ” = 0 xf00 , > 9 ” green ” = 0 x0f0 , > 10 ” blue ” = 0 x00f > 11 } 12 #= {” g r e e n ”=>240, ” b l u e ”=>15, ” r e d ”=>3840} > Alexander Boldt Ruby
  • 10. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Allgemeines::Syntax Ruby::Allgemeines::Syntax::Regul¨re Ausdr¨cke a u Listing 4: Regul¨re Ausdr¨cke a u 1 ## R e g u l a e r e A u s d r u e c k e 2 ” t h i s i s a t e s t ” . sub ( / [ a e i o u ] / , ’∗ ’) 3 # = th∗s i s a t e s t > 4 5 ” t h i s i s a t e s t ” . s p l i t (/ s / ) 6 # = [ ’ this ’ , ’ is ’ , ’a ’ , ’ test ’ ] > Alexander Boldt Ruby
  • 11. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Allgemeines::Flow Control Ruby::Allgemeines::Flow Control::If und Unless - elsif - case .. when Listing 5: Flow Control 1 # i f und u n l e s s , e l s e , c a s e . . when 2 a g e = 10 3 p u t s ” S i e s i n d zu j u n g f u e r d a s System ” i f age < 18 4 p u t s ” S i e s i n d zu j u n g f u e r d a s System ” u n l e s s age >= 18 5 6 i f a g e < 18 7 p u t s ” S i e s i n d zu j u n g f u e r d a s System ” 8 e l s i f a g e == 18 9 p u t s ” H a r t an d e r G r e n z e ” 10 else 11 p u t s ” S i e haben v o l l e n Z u g r i f f ” 12 end 13 14 c a s e age 15 when 1 . . 1 7 16 p u t s ” S i e s i n d zu j u n g f u e r d a s System ” 17 when 18 18 p u t s ” H a r t an d e r G r e n z e ” 19 else 20 p u t s ” S i e haben v o l l e n Z u g r i f f ” 21 end Alexander Boldt Ruby
  • 12. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Allgemeines::Flow Control Ruby::Allgemeines::Flow Control::upto, each, while, until Listing 6: upto - each - while - until 1 ## upto , each , w h i l e u n t i l . . . 2 1 . u p t o ( 5 ) { | number | p u t s number ∗2 } 3 4 x = [1 ,2 ,3 ,4] 5 x . each { | item | puts item} 6 7 x . e a c h do | i t e m | 8 puts ” A k t u e l l e r I n h a l t i s t #{i t e m }” 9 end 10 11 x = 1 12 w h i l e x < 100 # − u n t i l x > 99 > 13 puts x 14 x = x ∗ 2 15 end Alexander Boldt Ruby
  • 13. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Exception Handling::Verarbeitung ¨ Ruby::Exception Handling::Verarbeitung::Uberblick Die Oberklasse Exception besitzt uber 30 vordefinierte Subclassen ¨ ZeroDevisionError NoMethodError RuntimeError ArgumentError etc. Alexander Boldt Ruby
  • 14. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Exception Handling::Verarbeitung Ruby::Exception Handling::Verarbeitung::Ruby’s Weg Automatisch z.B. bei inkorrektem Aufruf von Funktionen Selbst definiert im Code (raise) Suche nach Handler f¨r jeweilige Exception (resque u NameDerException), wenn gefunden Ausf¨hrung, sonst u Hochreichen (resque Exception) oder sterben mit Error Meldung Alexander Boldt Ruby
  • 15. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Exception Handling::Sprachunterst¨tzung u Ruby::Exception Handling::Sprachunterst¨tzung::Eigene u Exceptions Listing 7: Raise - Resque - Eigene Exceptions 1 # Eigene Exceptions 2 c l a s s BadDataException < RuntimeError 3 end 4 5 c l a s s Person 6 d e f i n i t i a l i z e ( name ) 7 r a i s e B a d D a t a E x c e p t i o n , ” K e i n e Name” i f name . empty ? 8 end 9 end 10 11 begin 12 p = P e r s o n . new ( ” ” ) # − E r r o r > 13 r e s c u e BadDataException 14 p = P e r s o n . new ( ” D e f a u l t ” ) 15 end Alexander Boldt Ruby
  • 16. Ruby::Allgemeines Ruby::Exception Handling Abschluss Ruby::Exception Handling::Sprachunterst¨tzung u Ruby::Exception Handling::Sprachunterst¨tzung::Catch u and Throw Listing 8: Catch and Throw 1 # Ca tc h und Throw 2 c a t c h ( : f i n i s h ) do 3 1 0 0 0 . t i m e s do 4 x = rand (1000) 5 puts x 6 t h r o w : f i n i s h i f x == 123 7 end 8 p u t s ” G e n e r a t e d 1000 random numbers w i t h o u t g e n e r a t i n g 1 2 3 ! ” 9 end Alexander Boldt Ruby
  • 17. Ruby::Allgemeines Ruby::Exception Handling Abschluss Abschluss::Fragen?! Fragen?! Danke f¨r Eure Aufmerksamkeit. Bei Fragen stehe ich nun zur u Verf¨gung u Alexander Boldt Ruby