SlideShare una empresa de Scribd logo
1 de 71
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!

Más contenido relacionado

La actualidad más candente

The ruby programming language
The ruby programming languageThe ruby programming language
The ruby programming languagepraveen0202
 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 
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
 

La actualidad más candente (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 JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 
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
 
MMBJ Shanzhai Culture
MMBJ Shanzhai CultureMMBJ Shanzhai Culture
MMBJ Shanzhai Culture
 

Destacado

Newsletter #1 Spotrunner
Newsletter #1 SpotrunnerNewsletter #1 Spotrunner
Newsletter #1 Spotrunnerhazera01
 
Sukses un bi 1 th 2012 2013
Sukses un bi 1 th 2012 2013Sukses un bi 1 th 2012 2013
Sukses un bi 1 th 2012 2013Terry Brengost
 
用種菜,分享愛
用種菜,分享愛用種菜,分享愛
用種菜,分享愛advantech2012
 
Presentacion Final Control De Acceso Via Sms
Presentacion Final Control De Acceso Via SmsPresentacion Final Control De Acceso Via Sms
Presentacion Final Control De Acceso Via SmsAlexisEspin
 
formulir propolis
formulir propolisformulir propolis
formulir propolisdarsa19
 
Genre theory research
Genre theory researchGenre theory research
Genre theory research9mawj
 
Lista de cotejo para reporte
Lista de cotejo para reporteLista de cotejo para reporte
Lista de cotejo para reporteivan_antrax
 
El mol
El molEl mol
El molhhmmr
 
Copy of Untitled Presentation
Copy of Untitled PresentationCopy of Untitled Presentation
Copy of Untitled PresentationPat Longsiri
 
Storyboarding Practice
Storyboarding PracticeStoryboarding Practice
Storyboarding PracticeMizla
 
The final version with animations and sound3
The final version with animations and sound3The final version with animations and sound3
The final version with animations and sound3tim bowyer
 

Destacado (20)

Newsletter #1 Spotrunner
Newsletter #1 SpotrunnerNewsletter #1 Spotrunner
Newsletter #1 Spotrunner
 
Sukses un bi 1 th 2012 2013
Sukses un bi 1 th 2012 2013Sukses un bi 1 th 2012 2013
Sukses un bi 1 th 2012 2013
 
proyectsonyemir
proyectsonyemirproyectsonyemir
proyectsonyemir
 
用種菜,分享愛
用種菜,分享愛用種菜,分享愛
用種菜,分享愛
 
Presentacion Final Control De Acceso Via Sms
Presentacion Final Control De Acceso Via SmsPresentacion Final Control De Acceso Via Sms
Presentacion Final Control De Acceso Via Sms
 
formulir propolis
formulir propolisformulir propolis
formulir propolis
 
Genre theory research
Genre theory researchGenre theory research
Genre theory research
 
Lista de cotejo para reporte
Lista de cotejo para reporteLista de cotejo para reporte
Lista de cotejo para reporte
 
034mediacion carbonell 2010
034mediacion carbonell 2010034mediacion carbonell 2010
034mediacion carbonell 2010
 
Dq33705710
Dq33705710Dq33705710
Dq33705710
 
Donoso
DonosoDonoso
Donoso
 
El mol
El molEl mol
El mol
 
Power 5
Power 5Power 5
Power 5
 
Emilydowdybcc
EmilydowdybccEmilydowdybcc
Emilydowdybcc
 
Copy of Untitled Presentation
Copy of Untitled PresentationCopy of Untitled Presentation
Copy of Untitled Presentation
 
Readme
ReadmeReadme
Readme
 
Storyboarding Practice
Storyboarding PracticeStoryboarding Practice
Storyboarding Practice
 
The final version with animations and sound3
The final version with animations and sound3The final version with animations and sound3
The final version with animations and sound3
 
Toonpadessentiel2
Toonpadessentiel2Toonpadessentiel2
Toonpadessentiel2
 
Sortida Al Curs Alt2
Sortida  Al Curs Alt2Sortida  Al Curs Alt2
Sortida Al Curs Alt2
 

Similar a ppt18

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旻琦 潘
 

Similar a ppt18 (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
 

Más de 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
 

Más de 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
 

ppt18

  • 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!

Notas del editor

  1. Ruby for Perl programmers