Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 2 - OOP Maciej Mensfeld Presented by: Maciej Mensfeld More about OOP dev.mensfeld.pl github.com/mensfeld."— Presentation transcript:

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

2 Chapter 2 - OOP Maciej Mensfeld Classes

3 Chapter 2 - OOP Maciej Mensfeld 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.

4 Chapter 2 - OOP Maciej Mensfeld 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 ($).

5 Chapter 2 - OOP Maciej Mensfeld 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.

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

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

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

9 Chapter 2 - Access Control Maciej Mensfeld Access Control

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

11 Chapter 2 - Inheritance Maciej Mensfeld Class Inheritance This is how it works

12 Chapter 2 - Inheritance Maciej Mensfeld Class Inheritance 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.

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

14 Chapter 2 – writing some cool stuff Maciej Mensfeld Web crawler! Enough theory! Let’s be pragmatic! Simple web crawler requirements Fetch and store urls Don’t revisit urls Search 4 keywords (support regexp) Print results

15 Chapter 2 – writing some cool stuff Maciej Mensfeld Web crawler – page content parser What do we need? ParserCrawler Extracts data from page content Crawl all available pages

16 Chapter 2 – writing some cool stuff Maciej Mensfeld Simple parser – 13LOC attr_reader – set instance variable as readonly from the outside attr_accessor – make instanca variable R/W from the outside

17 Chapter 2 – writing some cool stuff Maciej Mensfeld Simple parser – 13LOC Try it out!

18 Chapter 2 – writing some cool stuff Maciej Mensfeld Crawler – How should it work? Try to download page Success? Select another page No Yes Do something with result (parse, send, etc) Mark page as visited

19 Chapter 2 – writing some cool stuff Maciej Mensfeld Crawler – 37LOC Try it out! Add our start url to a @new_urls array (basicly create @new_urls array with one address in it) No pages where visited yet – so @visited_urls = [] (empty array) We will check only pages with specified extensions (html, asp, php, etc)

20 Chapter 2 – writing some cool stuff Maciej Mensfeld20/23 Crawler – read page in Ruby Try it out! Mark page as visited (add it to @visited_urls array) Load current content page (parse URL and address) Catch any type of exception (404, 500, etc) – mark page as visited so we don’t go there again and return false Reading pages in Ruby is easy!

21 Chapter 2 – writing some cool stuff Maciej Mensfeld Crawler – extract URLs Try it out! Use URI.extract method to extract all urls from @current_content Check if URL is valid (try to parse it and check extension) If anything failes – assume that this URL is incorrect Reading pages in Ruby is easy!

22 Chapter 2 – writing some cool stuff Maciej Mensfeld Crawler – run crawler! Try it out!

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


Download ppt "Chapter 2 - OOP Maciej Mensfeld Presented by: Maciej Mensfeld More about OOP dev.mensfeld.pl github.com/mensfeld."

Similar presentations


Ads by Google