Download - Chapter 2 - OOP
Chapter 2 - OOP
Maciej Mensfeld
Presented by:
Maciej Mensfeld
More about OOP
github.com/mensfeld
Chapter 2 - OOP
Maciej Mensfeld
Classes
Chapter 2 - OOP
Maciej Mensfeld
Variables in a Ruby ClassRuby 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.
Chapter 2 - OOP
Maciej Mensfeld
Variables in a Ruby ClassRuby 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 ($).
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.
Chapter 2 - The accessors & setters
Maciej Mensfeld
The accessor & setter methods
Accessors
Chapter 2 - The accessors & setters
Maciej Mensfeld
The accessor & setter methods
Setters
Chapter 2 - The accessors & setters
Maciej Mensfeld
The accessor & setter methods
Setters + getters
Chapter 2 - Access Control
Maciej Mensfeld
Access Control
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.
Chapter 2 - Inheritance
Maciej Mensfeld
Class Inheritance
This is how it works
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.
Chapter 2 – let’s do programming!
Maciej Mensfeld
A bit of programming
Chapter 2 – writing some cool stuff
Maciej Mensfeld
Web crawler!
Enough theory!Let’s be pragmatic!
Simple web crawlerrequirements
Fetch and store urls
Don’t revisit urls
Search 4 keywords (support regexp)
Print results
Chapter 2 – writing some cool stuff
Maciej Mensfeld
Web crawler – page content parser
What do we need?
Parser Crawler
Extracts data from page content Crawl all available pages
Chapter 2 – writing some cool stuff
Maciej Mensfeld
Simple parser – 13LOC
attr_reader – set instance variable as readonly from the outsideattr_accessor – make instanca variable R/W from the outside
Chapter 2 – writing some cool stuff
Maciej Mensfeld
Simple parser – 13LOC
Try it out!
Chapter 2 – writing some cool stuff
Maciej Mensfeld
Crawler – How should it work?
Try to download page
Success?Select another
pageNo
Yes
Do something with result (parse, send, etc)
Mark page as visited
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)
Chapter 2 – writing some cool stuff
Maciej Mensfeld 20/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!
Chapter 2 – writing some cool stuff
Maciej Mensfeld
Crawler – extract URLs
Try it out!
Use URI.extract method to extract all urls from @current_contentCheck 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!
Chapter 2 – writing some cool stuff
Maciej Mensfeld
Crawler – run crawler!
Try it out!
Chapter 2 - OOP
Maciej Mensfeld
THX
Presented by:
Maciej Mensfeld
github.com/mensfeld