SlideShare une entreprise Scribd logo
1  sur  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!

Contenu connexe

Tendances (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
 
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
 

En vedette (7)

SXSW 2014 Implikasjoner - hva betyr trendene for deg og din forretning?
SXSW 2014 Implikasjoner - hva betyr trendene for deg og din forretning? SXSW 2014 Implikasjoner - hva betyr trendene for deg og din forretning?
SXSW 2014 Implikasjoner - hva betyr trendene for deg og din forretning?
 
Automation 1
Automation 1Automation 1
Automation 1
 
2443 Photos Aeriennes Du 11 Sept.Pps
2443 Photos Aeriennes Du 11 Sept.Pps2443 Photos Aeriennes Du 11 Sept.Pps
2443 Photos Aeriennes Du 11 Sept.Pps
 
Powerpointfinal
PowerpointfinalPowerpointfinal
Powerpointfinal
 
Defense
DefenseDefense
Defense
 
Twitterprofil
TwitterprofilTwitterprofil
Twitterprofil
 
Downtown Reclaim the Streets
Downtown Reclaim the StreetsDowntown Reclaim the Streets
Downtown Reclaim the Streets
 

Similaire à name name2 n2

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
Oregon Law Practice Management
 
Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1
Alejandra Perez
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
ppparthpatel123
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
旻琦 潘
 

Similaire à name name2 n2 (20)

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
 
Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1
 
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
 
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
 

Plus 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
 

Dernier

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Dernier (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

name name2 n2

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

Notes de l'éditeur

  1. Ruby for Perl programmers