1 / 23

Chapter 2 - OOP

Chapter 2 - OOP. More about OOP. Presented b y :. Maciej Mensfeld. maciej@mensfeld.pl dev.mensfeld.pl github.com / mensfeld. Maciej Mensfeld. Chapter 2 - OOP. Classes. Maciej Mensfeld. Chapter 2 - OOP. Variables in a Ruby Class. Ruby provides four types of variables:.

vivi
Download Presentation

Chapter 2 - OOP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 - OOP More about OOP Presented by: Maciej Mensfeld maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld

  2. Chapter 2 - OOP Classes Maciej Mensfeld

  3. Chapter 2 - OOP Variables in a Ruby Class Ruby provides four types of variables: Local Variables: Local variables are the variables that are defined in a method. Local variables are not available outside the method. Instance Variables: Instance variables are available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by the at sign (@) followed by the variable name. Maciej Mensfeld

  4. Chapter 2 - OOP Variables in a Ruby Class Ruby provides four types of variables: Class Variables: Class variables are available across different objects. A class variable belongs to the class and is a characteristic of a class. They are preceded by the sign @@ and are followed by the variable name. Global Variables: Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. The global variables are always preceded by the dollar sign ($). Maciej Mensfeld

  5. Chapter 2 - OOP Custom Method to create Objects When you plan to declare the new method with parameters, you need to declare the method initialize at the time of the class creation. The initialize method is a special type of method, which will be executed when the new method of the class is called with parameters. Maciej Mensfeld

  6. Chapter 2 - The accessors & setters The accessor & setter methods Accessors Maciej Mensfeld

  7. Chapter 2 - The accessors & setters The accessor & setter methods Setters Maciej Mensfeld

  8. Chapter 2 - The accessors & setters The accessor & setter methods Setters + getters Maciej Mensfeld

  9. Chapter 2 - Access Control Access Control Maciej Mensfeld

  10. Chapter 2 - Access Control Access Control Public Methods: Public methods can be called by anyone. Methods are public by default except for initialize, which is always private. Protected Methods: A protected method can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. Private Methods: Private methods cannot be accessed, or even viewed from outside the class. Only the class methods can access private members. Maciej Mensfeld

  11. Chapter 2 - Inheritance ClassInheritance Thisishowitworks Maciej Mensfeld

  12. Chapter 2 - Inheritance ClassInheritance Ruby does not support Multiple level of inheritances but Ruby supports mixins. A mixin is like a specialized implementation of multiple inheritance in which only the interface portion is inherited. Maciej Mensfeld

  13. Chapter 2 – let’s do programming! A bit of programming Maciej Mensfeld

  14. Chapter 2 – writingsomecoolstuff Web crawler! Enoughtheory! Let’s be pragmatic! Fetch and storeurls Simple webcrawler requirements Search 4 keywords (supportregexp) Don’trevisiturls Printresults Maciej Mensfeld

  15. Chapter 2 – writingsomecoolstuff Web crawler – pagecontentparser What do we need? Parser Crawler Extracts data frompagecontent Crawl allavailablepages Maciej Mensfeld

  16. Chapter 2 – writingsomecoolstuff Simple parser – 13LOC attr_reader – set instancevariable as readonlyfromtheoutside attr_accessor – make instancavariable R/W fromtheoutside Maciej Mensfeld

  17. Chapter 2 – writingsomecoolstuff Simple parser – 13LOC Tryit out! Maciej Mensfeld

  18. Chapter 2 – writingsomecoolstuff Crawler – Howshoulditwork? Try to downloadpage Success? Selectanotherpage No Yes Do somethingwithresult (parse, send, etc) Mark page as visited Maciej Mensfeld

  19. Chapter 2 – writingsomecoolstuff Crawler – 37LOC Tryit out! We will checkonlypageswithspecifiedextensions (html, asp, php, etc) Addour start url to a @new_urlsarray (basiclycreate @new_urlsarraywith one addressinit) No pageswherevisitedyet – so @visited_urls = [] (emptyarray) Maciej Mensfeld

  20. Chapter 2 – writingsomecoolstuff Crawler – readpagein Ruby Reading pagesin Ruby iseasy! Tryit out! Mark page as visited (addit to @visited_urlsarray) Loadcurrentcontentpage (parse URL and address) Catchanytype of exception (404, 500, etc) – markpage as visited so we don’t go thereagain and return false Maciej Mensfeld 20/23

  21. Chapter 2 – writingsomecoolstuff Crawler – extractURLs Reading pagesin Ruby iseasy! Tryit out! UseURI.extractmethod to extractallurlsfrom @current_content Checkif URL isvalid (try to parseit and checkextension) Ifanythingfailes – assumethatthis URL isincorrect Maciej Mensfeld

  22. Chapter 2 – writingsomecoolstuff Crawler – run crawler! Tryit out! Maciej Mensfeld

  23. Chapter 2 - OOP THX Presented by: Maciej Mensfeld maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld

More Related