+ All Categories
Home > Documents > Chapter 2 - OOP

Chapter 2 - OOP

Date post: 24-Feb-2016
Category:
Upload: vivi
View: 82 times
Download: 0 times
Share this document with a friend
Description:
Chapter 2 - OOP. More about OOP. Presented b y :. Maciej Mensfeld. [email protected] 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:. - PowerPoint PPT Presentation
Popular Tags:
23
Chapter 2 - OOP Maciej Mensfeld Presented by: Maciej Mensfeld More about OOP [email protected] dev.mensfeld.pl github.com/mensfeld
Transcript
Page 1: Chapter  2 - OOP

Chapter 2 - OOP

Maciej Mensfeld

Presented by:

Maciej Mensfeld

More about OOP

[email protected]

github.com/mensfeld

Page 2: Chapter  2 - OOP

Chapter 2 - OOP

Maciej Mensfeld

Classes

Page 3: Chapter  2 - OOP

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.

Page 4: Chapter  2 - OOP

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 ($).

Page 5: Chapter  2 - OOP

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.

Page 6: Chapter  2 - OOP

Chapter 2 - The accessors & setters

Maciej Mensfeld

The accessor & setter methods

Accessors

Page 7: Chapter  2 - OOP

Chapter 2 - The accessors & setters

Maciej Mensfeld

The accessor & setter methods

Setters

Page 8: Chapter  2 - OOP

Chapter 2 - The accessors & setters

Maciej Mensfeld

The accessor & setter methods

Setters + getters

Page 9: Chapter  2 - OOP

Chapter 2 - Access Control

Maciej Mensfeld

Access Control

Page 10: Chapter  2 - OOP

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.

Page 11: Chapter  2 - OOP

Chapter 2 - Inheritance

Maciej Mensfeld

Class Inheritance

This is how it works

Page 12: Chapter  2 - OOP

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.

Page 13: Chapter  2 - OOP

Chapter 2 – let’s do programming!

Maciej Mensfeld

A bit of programming

Page 14: Chapter  2 - OOP

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

Page 15: Chapter  2 - OOP

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

Page 16: Chapter  2 - OOP

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

Page 17: Chapter  2 - OOP

Chapter 2 – writing some cool stuff

Maciej Mensfeld

Simple parser – 13LOC

Try it out!

Page 18: Chapter  2 - OOP

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

Page 19: Chapter  2 - OOP

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)

Page 20: Chapter  2 - OOP

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!

Page 21: Chapter  2 - OOP

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!

Page 22: Chapter  2 - OOP

Chapter 2 – writing some cool stuff

Maciej Mensfeld

Crawler – run crawler!

Try it out!

Page 23: Chapter  2 - OOP

Chapter 2 - OOP

Maciej Mensfeld

THX

Presented by:

Maciej Mensfeld

[email protected]

github.com/mensfeld


Recommended