240 likes | 452 Views
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:.
E N D
Chapter 2 - OOP More about OOP Presented by: 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: 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
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
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
Chapter 2 - The accessors & setters The accessor & setter methods Accessors Maciej Mensfeld
Chapter 2 - The accessors & setters The accessor & setter methods Setters Maciej Mensfeld
Chapter 2 - The accessors & setters The accessor & setter methods Setters + getters Maciej Mensfeld
Chapter 2 - Access Control Access Control Maciej Mensfeld
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
Chapter 2 - Inheritance ClassInheritance Thisishowitworks Maciej Mensfeld
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
Chapter 2 – let’s do programming! A bit of programming Maciej Mensfeld
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
Chapter 2 – writingsomecoolstuff Web crawler – pagecontentparser What do we need? Parser Crawler Extracts data frompagecontent Crawl allavailablepages Maciej Mensfeld
Chapter 2 – writingsomecoolstuff Simple parser – 13LOC attr_reader – set instancevariable as readonlyfromtheoutside attr_accessor – make instancavariable R/W fromtheoutside Maciej Mensfeld
Chapter 2 – writingsomecoolstuff Simple parser – 13LOC Tryit out! Maciej Mensfeld
Chapter 2 – writingsomecoolstuff Crawler – Howshoulditwork? Try to downloadpage Success? Selectanotherpage No Yes Do somethingwithresult (parse, send, etc) Mark page as visited Maciej Mensfeld
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
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
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
Chapter 2 – writingsomecoolstuff Crawler – run crawler! Tryit out! Maciej Mensfeld
Chapter 2 - OOP THX Presented by: Maciej Mensfeld maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld