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

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

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

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

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
 

Último

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Último (20)

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...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

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!

Notas del editor

  1. Ruby for Perl programmers