SlideShare ist ein Scribd-Unternehmen logo
1 von 71
Downloaden Sie, um offline zu lesen
Ruby for Perl programmers for Perl programmers Ruby All material Copyright Hal E. Fulton, 2002. Use freely but acknowledge the source.
What is Ruby? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Ruby is  General-Purpose ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A Brief History of Ruby… ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Where does Ruby get its ideas? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Ruby’s Design Principles ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Disclaimer: ,[object Object],[object Object]
How is Ruby like Perl? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
How is Ruby different from Perl? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Some Specific Similarities… ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Some Specific Differences… ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Enough! Let’s see some code. ,[object Object],[object Object]
Some Basic Syntax ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Some More Syntax ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Loops   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Conditions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Loop and condition modifier forms ,[object Object],[object Object],[object Object],[object Object]
Syntax Sugar and More ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
OOP in Ruby ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A Simple Class class Person def initialize(name, number)   @name, @phone = name, number end   def inspect “ Name=#{@name} Phone=#{@phone}” end end # Note that “new” invokes “initialize” a1 = Person.new(“Bill Gates”, “1-800-666-0666”) a2 = Person.new(“Jenny”, “867-5309”) # p is like print or puts but invokes inspect p a2  # Prints “Name=Jenny Phone=867-5309”
Defining attributes # Adding to previous example… class Person attr_reader :name  # Defines a “name” method attr_accessor :phone  # Defines “phone” and   # “phone=“ methods end person1 = a2.name  # “Jenny” phone_num = a2.phone  # “867-5309” a2.phone = “512 867-5309” # Value replaced… p a2  # Prints “Name=Jenny Phone=512 867-5309”
Class-level entities class Foobar @@count = 0  # Class variable def initialize(str)   @data = str @@count += 1 end   def Foobar.population  # Class method @@count end end a = Foobar.new(“lions”) b = Foobar.new(“tigers”) c = Foobar.new(“bears”) puts Foobar.population  # Prints 3
Inheritance class Student < Person def initialize(name, number, id, major)   @name, @phone = name, number @id, @major = id, major end   def inspect super + “ID=#@id Major=#@major” end end x = Student.new(“Mike Nicholas”, “555-1234”, “ 000-13-5031”, “physics”) puts “yes” if x.is_a? Student  # yes puts “yes” if x.is_a? Person  # yes
Singleton objects # Assume a “Celebrity” class newsguy = Celebrity.new(“Dan Rather”) popstar = Celebrity.new(“Britney Spears”) superman = Celebrity.new(“Superman”) class << superman def fly puts “Look, I’m flying! Woo-hoo!” end end superman.fly  # Look, I’m flying! Woo-hoo! newsguy.fly  # Error!
Garbage Collection ,[object Object],[object Object],[object Object],[object Object]
Using  method_missing class OSwrapper def method_missing(method, *args) system(method.to_s, *args) end end sys = OSwrapper.new sys.date  # Sat Apr 13 16:32:00… sys.du “-s”, “/tmp”  # 166749 /tmp
Modules in Ruby ,[object Object],[object Object],[object Object]
Modules as mixins ,[object Object],[object Object],[object Object]
Modules for Interface Polymorphism ,[object Object],[object Object]
Modules for Namespace Management ,[object Object],[object Object],[object Object],[object Object]
Module example 1 class MyCollection include Enumerable  # interface polymorphism #… def <=>(other) # Compare self to other somehow… # Return –1, 0, or 1 end def each # Iterate through the collection and do # a yield on each one… end end
Module example 2 # Namespace management def sin puts “Pride, gluttony, bad commenting…” end x = Math.sin(theta)  # unrelated to “our” sin User = Process.uid  # uid of this process
Module example 3 # A user-defined module module FlyingThing def fly   puts “Look, I’m flying!” end end class Bat < Mammal include FlyingThing  # A substitute for MI? #… end x = Bat.new x.is_a? Bat  # true x.is_a? Mammal  # true x.is_a? FlyingThing  # true
Programming Paradigms ,[object Object],[object Object],[object Object],[object Object],[object Object]
Cool Features in Ruby ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A Rich Set of Classes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A Closer Look at Some Classes… ,[object Object],[object Object],[object Object]
Iterators ,[object Object],[object Object],[object Object]
Iterators That Don’t Iterate ,[object Object],[object Object]
Defining Your Own Iterators ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Interesting Example #1 ,[object Object],[object Object],[object Object],[object Object],[object Object]
Interesting Example #2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Open Classes ,[object Object],[object Object],[object Object]
Exceptions ,[object Object],[object Object],[object Object]
Catching Exceptions, Part 1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Catching Exceptions, Part 2:  The General Form ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Other Forms of  rescue ,[object Object],[object Object],[object Object]
Easy Extension (in Ruby) ,[object Object],[object Object],[object Object]
Example: Smalltalk-like  inject ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Invert Array to Form Hash ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Sorting by an Arbitrary Key ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Example: Existential Quantifiers module Quantifier  def any?  self.each { |x| return true if yield x }  false  end  def all?  self.each { |x| return false if not yield x }  true  end  end  class Array include Quantifier end list = [1, 2, 3, 4, 5]  flag1 = list.any? {|x| x > 5 }  # false  flag2 = list.any? {|x| x >= 5 }  # true  flag3 = list.all? {|x| x <= 10 }  # true  flag4 = list.all? {|x| x % 2 == 0 }  # false
Example: Selective File Deletion ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Other Possible Examples (of Extending Ruby in Ruby) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Dynamic Features of Ruby ,[object Object],[object Object],[object Object],[object Object]
Operator Overloading ,[object Object],[object Object]
Operator Overloading, ex. 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
The  Bignum  class ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Threads in Ruby ,[object Object],[object Object],[object Object],[object Object],[object Object]
Thread Example 1 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Thread Example 2 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Continuations ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Extending Ruby in C ,[object Object],[object Object],[object Object],[object Object],[object Object]
Ruby’s Weaknesses ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Libraries and Utilities ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Ruby and MS Windows ,[object Object],[object Object],[object Object],[object Object]
Who’s Into Ruby… ,[object Object],[object Object],[object Object],[object Object]
Web Resources ,[object Object],[object Object],[object Object],[object Object]
Print Resources ,[object Object],[object Object],[object Object],[object Object],[object Object]
The Ruby Way Table of Contents 1.  Ruby in Review 2.  Simple Data Tasks 3.  Manipulating Structured Data 4.  External Data Manipulation 5.  OOP and Dynamicity in Ruby 6.  Graphical Interfaces for Ruby 7.  Ruby Threads 8.  Scripting and System Administration 9.  Network and Web Programming A.  From Perl to Ruby B.  From Python to Ruby C.  Tools and Utilities D.  Resources on the Web (and Elsewhere) E.  What’s New in Ruby 1.8 More than 300 sections More than 500 code fragments and full listings More than 10,000 lines of code All significant code fragments available in an archive
exit(0)  # That’s all, folks!

Weitere ähnliche Inhalte

Was ist angesagt?

The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming languagepraveen0202
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
Jerry Shea Resume And Addendum 5 2 09
Jerry  Shea Resume And Addendum 5 2 09Jerry  Shea Resume And Addendum 5 2 09
Jerry Shea Resume And Addendum 5 2 09gshea11
 

Was ist angesagt? (8)

Introducing Ruby
Introducing RubyIntroducing Ruby
Introducing Ruby
 
The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming language
 
Ruby
RubyRuby
Ruby
 
Javascript
JavascriptJavascript
Javascript
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
MMBJ Shanzhai Culture
MMBJ Shanzhai CultureMMBJ Shanzhai Culture
MMBJ Shanzhai Culture
 
Jerry Shea Resume And Addendum 5 2 09
Jerry  Shea Resume And Addendum 5 2 09Jerry  Shea Resume And Addendum 5 2 09
Jerry Shea Resume And Addendum 5 2 09
 

Andere mochten auch

Nuovo Progetto per Campo - Elezioni 2014
Nuovo Progetto per Campo - Elezioni 2014Nuovo Progetto per Campo - Elezioni 2014
Nuovo Progetto per Campo - Elezioni 2014Emiliano Provenzali
 
Il sociale incontra il territorio
Il sociale incontra il territorioIl sociale incontra il territorio
Il sociale incontra il territoriocoriolano giorgi
 
06 - Giorno Della Memoria - Gennaio 27
06 - Giorno Della Memoria - Gennaio 2706 - Giorno Della Memoria - Gennaio 27
06 - Giorno Della Memoria - Gennaio 27Istituto Comprensivo
 
Dino cristanini, bilancio sociale
Dino cristanini, bilancio socialeDino cristanini, bilancio sociale
Dino cristanini, bilancio socialesepulvi
 
Novità Saggistica dicembre 2011
Novità Saggistica dicembre 2011Novità Saggistica dicembre 2011
Novità Saggistica dicembre 2011BibliotecaQC
 
Illustrazione e commento_legge_fornero
Illustrazione e commento_legge_forneroIllustrazione e commento_legge_fornero
Illustrazione e commento_legge_fornerofspice
 
Corso educatore cinofilo
Corso educatore cinofiloCorso educatore cinofilo
Corso educatore cinofilonightdawn
 
Strategia Della Tensione
Strategia Della TensioneStrategia Della Tensione
Strategia Della Tensionerobertnozick
 
Strutture sportive sovracomunali - #PTC
Strutture sportive sovracomunali - #PTCStrutture sportive sovracomunali - #PTC
Strutture sportive sovracomunali - #PTCAlessio Migazzi
 
L'uomo e l'energia Leila Orlando
L'uomo e l'energia Leila OrlandoL'uomo e l'energia Leila Orlando
L'uomo e l'energia Leila OrlandoLeila Orlando
 
Opinione: Al cambiamento servono leader
Opinione: Al cambiamento servono leaderOpinione: Al cambiamento servono leader
Opinione: Al cambiamento servono leaderRoberto Siagri
 
Lezione di economia3
Lezione di economia3Lezione di economia3
Lezione di economia3Gilda Tobia
 
Risi spe com_presentazione_4_06_slide
Risi spe com_presentazione_4_06_slideRisi spe com_presentazione_4_06_slide
Risi spe com_presentazione_4_06_slideRisi Elisabetta
 
Sophie Ernst: Auschwitz dans la salle de classe. Enseigner l'histoire ou le d...
Sophie Ernst: Auschwitz dans la salle de classe. Enseigner l'histoire ou le d...Sophie Ernst: Auschwitz dans la salle de classe. Enseigner l'histoire ou le d...
Sophie Ernst: Auschwitz dans la salle de classe. Enseigner l'histoire ou le d...INSMLI
 
Programma 1: UDINE CUORE DELL'INNOVAZIONE
Programma 1: UDINE CUORE DELL'INNOVAZIONEProgramma 1: UDINE CUORE DELL'INNOVAZIONE
Programma 1: UDINE CUORE DELL'INNOVAZIONEComune Udine
 

Andere mochten auch (20)

Nuovo Progetto per Campo - Elezioni 2014
Nuovo Progetto per Campo - Elezioni 2014Nuovo Progetto per Campo - Elezioni 2014
Nuovo Progetto per Campo - Elezioni 2014
 
Il sociale incontra il territorio
Il sociale incontra il territorioIl sociale incontra il territorio
Il sociale incontra il territorio
 
Art Nouveau
Art NouveauArt Nouveau
Art Nouveau
 
06 - Giorno Della Memoria - Gennaio 27
06 - Giorno Della Memoria - Gennaio 2706 - Giorno Della Memoria - Gennaio 27
06 - Giorno Della Memoria - Gennaio 27
 
A.fortunati udine 22 giu 2011
A.fortunati udine 22 giu 2011A.fortunati udine 22 giu 2011
A.fortunati udine 22 giu 2011
 
Dino cristanini, bilancio sociale
Dino cristanini, bilancio socialeDino cristanini, bilancio sociale
Dino cristanini, bilancio sociale
 
Novità Saggistica dicembre 2011
Novità Saggistica dicembre 2011Novità Saggistica dicembre 2011
Novità Saggistica dicembre 2011
 
Illustrazione e commento_legge_fornero
Illustrazione e commento_legge_forneroIllustrazione e commento_legge_fornero
Illustrazione e commento_legge_fornero
 
Corso educatore cinofilo
Corso educatore cinofiloCorso educatore cinofilo
Corso educatore cinofilo
 
Strategia Della Tensione
Strategia Della TensioneStrategia Della Tensione
Strategia Della Tensione
 
Strutture sportive sovracomunali - #PTC
Strutture sportive sovracomunali - #PTCStrutture sportive sovracomunali - #PTC
Strutture sportive sovracomunali - #PTC
 
Il castello di Castelletto
Il castello di CastellettoIl castello di Castelletto
Il castello di Castelletto
 
17 Maggio Venezia Mapelli
17 Maggio Venezia Mapelli17 Maggio Venezia Mapelli
17 Maggio Venezia Mapelli
 
Programma 15
Programma 15Programma 15
Programma 15
 
L'uomo e l'energia Leila Orlando
L'uomo e l'energia Leila OrlandoL'uomo e l'energia Leila Orlando
L'uomo e l'energia Leila Orlando
 
Opinione: Al cambiamento servono leader
Opinione: Al cambiamento servono leaderOpinione: Al cambiamento servono leader
Opinione: Al cambiamento servono leader
 
Lezione di economia3
Lezione di economia3Lezione di economia3
Lezione di economia3
 
Risi spe com_presentazione_4_06_slide
Risi spe com_presentazione_4_06_slideRisi spe com_presentazione_4_06_slide
Risi spe com_presentazione_4_06_slide
 
Sophie Ernst: Auschwitz dans la salle de classe. Enseigner l'histoire ou le d...
Sophie Ernst: Auschwitz dans la salle de classe. Enseigner l'histoire ou le d...Sophie Ernst: Auschwitz dans la salle de classe. Enseigner l'histoire ou le d...
Sophie Ernst: Auschwitz dans la salle de classe. Enseigner l'histoire ou le d...
 
Programma 1: UDINE CUORE DELL'INNOVAZIONE
Programma 1: UDINE CUORE DELL'INNOVAZIONEProgramma 1: UDINE CUORE DELL'INNOVAZIONE
Programma 1: UDINE CUORE DELL'INNOVAZIONE
 

Ähnlich wie ppt2

Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi PetangMajlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi PetangImsamad
 
Agapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.comAgapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.comAntonio Silva
 
Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Alejandra Perez
 
Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5Oregon Law Practice Management
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technologyppparthpatel123
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 
F# and Reactive Programming for iOS
F# and Reactive Programming for iOSF# and Reactive Programming for iOS
F# and Reactive Programming for iOSBrad Pillow
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting languageVamshi Santhapuri
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Wsloffenauer
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introductionGonçalo Silva
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionProf. Wim Van Criekinge
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends旻琦 潘
 

Ähnlich wie ppt2 (20)

Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi PetangMajlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
Majlis Persaraan Pn.Hjh.Normah bersama guru-guru Sesi Petang
 
Agapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.comAgapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.com
 
LoteríA Correcta
LoteríA CorrectaLoteríA Correcta
LoteríA Correcta
 
Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1
 
Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5
 
Ruby
RubyRuby
Ruby
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 
F# and Reactive Programming for iOS
F# and Reactive Programming for iOSF# and Reactive Programming for iOS
F# and Reactive Programming for iOS
 
Introduction to perl_ a scripting language
Introduction to perl_ a scripting languageIntroduction to perl_ a scripting language
Introduction to perl_ a scripting language
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
 
Ruby — An introduction
Ruby — An introductionRuby — An introduction
Ruby — An introduction
 
Ruby Hell Yeah
Ruby Hell YeahRuby Hell Yeah
Ruby Hell Yeah
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Introduction to Perl Programming
Introduction to Perl ProgrammingIntroduction to Perl Programming
Introduction to Perl Programming
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
Bash production guide
Bash production guideBash production guide
Bash production guide
 

Mehr von callroom

Test Presentation
Test PresentationTest Presentation
Test Presentationcallroom
 
Thyroid and the Heart
Thyroid and the HeartThyroid and the Heart
Thyroid and the Heartcallroom
 
Myocardial Viability - the STICH Trial NEJM May 2011
Myocardial Viability - the STICH Trial NEJM May 2011Myocardial Viability - the STICH Trial NEJM May 2011
Myocardial Viability - the STICH Trial NEJM May 2011callroom
 
Flail Leaflet
Flail LeafletFlail Leaflet
Flail Leafletcallroom
 
Fat versus Fit
Fat versus FitFat versus Fit
Fat versus Fitcallroom
 
Cardiac MR and viability
Cardiac MR and viabilityCardiac MR and viability
Cardiac MR and viabilitycallroom
 
Cardiac MR and viability
Cardiac MR and viabilityCardiac MR and viability
Cardiac MR and viabilitycallroom
 
LFT Review
LFT ReviewLFT Review
LFT Reviewcallroom
 
testing123
testing123testing123
testing123callroom
 
Hypertrophic Cardiomyopathy
Hypertrophic CardiomyopathyHypertrophic Cardiomyopathy
Hypertrophic Cardiomyopathycallroom
 
C. diff presentation
C. diff presentationC. diff presentation
C. diff presentationcallroom
 
Hemostasis and Thrombosis
Hemostasis and ThrombosisHemostasis and Thrombosis
Hemostasis and Thrombosiscallroom
 

Mehr von callroom (20)

ppt6
ppt6ppt6
ppt6
 
PPT5
PPT5PPT5
PPT5
 
PPT2
PPT2PPT2
PPT2
 
PPT1
PPT1PPT1
PPT1
 
Test Presentation
Test PresentationTest Presentation
Test Presentation
 
Thyroid and the Heart
Thyroid and the HeartThyroid and the Heart
Thyroid and the Heart
 
Myocardial Viability - the STICH Trial NEJM May 2011
Myocardial Viability - the STICH Trial NEJM May 2011Myocardial Viability - the STICH Trial NEJM May 2011
Myocardial Viability - the STICH Trial NEJM May 2011
 
Flail Leaflet
Flail LeafletFlail Leaflet
Flail Leaflet
 
Fat versus Fit
Fat versus FitFat versus Fit
Fat versus Fit
 
Cardiac MR and viability
Cardiac MR and viabilityCardiac MR and viability
Cardiac MR and viability
 
Cardiac MR and viability
Cardiac MR and viabilityCardiac MR and viability
Cardiac MR and viability
 
LFT Review
LFT ReviewLFT Review
LFT Review
 
testing123
testing123testing123
testing123
 
Hypertrophic Cardiomyopathy
Hypertrophic CardiomyopathyHypertrophic Cardiomyopathy
Hypertrophic Cardiomyopathy
 
C. diff presentation
C. diff presentationC. diff presentation
C. diff presentation
 
test
testtest
test
 
Hemostasis and Thrombosis
Hemostasis and ThrombosisHemostasis and Thrombosis
Hemostasis and Thrombosis
 
 
 
qwqsqw
qwqsqwqwqsqw
qwqsqw
 

ppt2

  • 1. Ruby for Perl programmers for Perl programmers Ruby All material Copyright Hal E. Fulton, 2002. Use freely but acknowledge the source.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20. A Simple Class class Person def initialize(name, number) @name, @phone = name, number end def inspect “ Name=#{@name} Phone=#{@phone}” end end # Note that “new” invokes “initialize” a1 = Person.new(“Bill Gates”, “1-800-666-0666”) a2 = Person.new(“Jenny”, “867-5309”) # p is like print or puts but invokes inspect p a2 # Prints “Name=Jenny Phone=867-5309”
  • 21. Defining attributes # Adding to previous example… class Person attr_reader :name # Defines a “name” method attr_accessor :phone # Defines “phone” and # “phone=“ methods end person1 = a2.name # “Jenny” phone_num = a2.phone # “867-5309” a2.phone = “512 867-5309” # Value replaced… p a2 # Prints “Name=Jenny Phone=512 867-5309”
  • 22. Class-level entities class Foobar @@count = 0 # Class variable def initialize(str) @data = str @@count += 1 end def Foobar.population # Class method @@count end end a = Foobar.new(“lions”) b = Foobar.new(“tigers”) c = Foobar.new(“bears”) puts Foobar.population # Prints 3
  • 23. Inheritance class Student < Person def initialize(name, number, id, major) @name, @phone = name, number @id, @major = id, major end def inspect super + “ID=#@id Major=#@major” end end x = Student.new(“Mike Nicholas”, “555-1234”, “ 000-13-5031”, “physics”) puts “yes” if x.is_a? Student # yes puts “yes” if x.is_a? Person # yes
  • 24. Singleton objects # Assume a “Celebrity” class newsguy = Celebrity.new(“Dan Rather”) popstar = Celebrity.new(“Britney Spears”) superman = Celebrity.new(“Superman”) class << superman def fly puts “Look, I’m flying! Woo-hoo!” end end superman.fly # Look, I’m flying! Woo-hoo! newsguy.fly # Error!
  • 25.
  • 26. Using method_missing class OSwrapper def method_missing(method, *args) system(method.to_s, *args) end end sys = OSwrapper.new sys.date # Sat Apr 13 16:32:00… sys.du “-s”, “/tmp” # 166749 /tmp
  • 27.
  • 28.
  • 29.
  • 30.
  • 31. Module example 1 class MyCollection include Enumerable # interface polymorphism #… def <=>(other) # Compare self to other somehow… # Return –1, 0, or 1 end def each # Iterate through the collection and do # a yield on each one… end end
  • 32. Module example 2 # Namespace management def sin puts “Pride, gluttony, bad commenting…” end x = Math.sin(theta) # unrelated to “our” sin User = Process.uid # uid of this process
  • 33. Module example 3 # A user-defined module module FlyingThing def fly puts “Look, I’m flying!” end end class Bat < Mammal include FlyingThing # A substitute for MI? #… end x = Bat.new x.is_a? Bat # true x.is_a? Mammal # true x.is_a? FlyingThing # true
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52. Example: Existential Quantifiers module Quantifier def any? self.each { |x| return true if yield x } false end def all? self.each { |x| return false if not yield x } true end end class Array include Quantifier end list = [1, 2, 3, 4, 5] flag1 = list.any? {|x| x > 5 } # false flag2 = list.any? {|x| x >= 5 } # true flag3 = list.all? {|x| x <= 10 } # true flag4 = list.all? {|x| x % 2 == 0 } # false
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70. The Ruby Way Table of Contents 1. Ruby in Review 2. Simple Data Tasks 3. Manipulating Structured Data 4. External Data Manipulation 5. OOP and Dynamicity in Ruby 6. Graphical Interfaces for Ruby 7. Ruby Threads 8. Scripting and System Administration 9. Network and Web Programming A. From Perl to Ruby B. From Python to Ruby C. Tools and Utilities D. Resources on the Web (and Elsewhere) E. What’s New in Ruby 1.8 More than 300 sections More than 500 code fragments and full listings More than 10,000 lines of code All significant code fragments available in an archive
  • 71. exit(0) # That’s all, folks!

Hinweis der Redaktion

  1. Ruby for Perl programmers