ruby: an introduction - who am i?
DESCRIPTION
Ruby: An introduction - Who am I?. Ruby: An introduction . Presented b y :. Maciej Mensfeld. senior ruby [email protected] lead ruby [email protected]. [email protected] dev.mensfeld.pl github.com / mensfeld. Maciej Mensfeld. 1/23. Ruby: An introduction – please …. - PowerPoint PPT PresentationTRANSCRIPT
Ruby: An introduction - Who am I?
Maciej Mensfeld 1/23
Presented by:
Maciej Mensfeld
Ruby: An introduction
github.com/mensfeld
senior ruby [email protected] ruby [email protected]
Ruby: An introduction – please…
Maciej Mensfeld 2/23
Please…
• …ask me to slow down, if I speak to quickly;• …ask me again, if I forget;
• …ask questions, if anything i say is not clear;• …feel free to share your own observations
Ruby: An introduction
Ruby: An introduction – What is Ruby?
Maciej Mensfeld 4/23
What is Ruby?
• Pure object-oriented programming language (even the number 1 is an instance of class);
• Created by Yukihiro Matsumoto in 1993;• Freely available and open-source;• Syntax is readable and easy to learn;• Being used for text processing, web apps, general system
administration, and AI and math research.• Can be extended with Ruby or low-level C;• Really helpful community;
Ruby: An introduction – What I love in Ruby?
Maciej Mensfeld 5/23
Clarity not ceremony – Main program
Java:public class HelloWorld{ public static void main(String args){ System.out.println(„Hello World”); }}
Ruby:puts „Hello World”
Try it out!
Ruby: An introduction – What I love in Ruby?
Maciej Mensfeld 6/23
Expressive syntax && objects, objects, objects…
3.times { puts „Ruby is cool”}[„Maciek”, „John”, „Anna”].first #=> „Maciek”[„Maciek”, „John”, „Anna”].last #=> „Anna”attr_accessor :name
„Anna”.class #=> Stringnil.class #=> NilClass1.class #=> Integer{}.class #=> Hash[].class #=> Arrayself.class #=> Object(0..9).class #=> Range
Ruby: An introduction – syntax
Maciej Mensfeld 7/23
Ruby syntax – hello world as a function
Hello World! puts „Hello World!”def h puts „Hello World!”end
h => „Hello World!”
Hello YourName! puts „Hello #{name}”
def h(name=„World”) puts „Hello #{name}!”end
h („Maciek”)=> „Hello Maciek!”
Try it out!
Ruby: An introduction – syntax
Maciej Mensfeld 8/23
Ruby syntax – classes, methods, objects
Hello YourName! as an object
# Comments starts with „#”class Messenger def initialize(name) # instance variables starts with „@” @name = name end
public def hello puts „Hello #{@name }!” endend
msg = Message.new(„Maciek”)msg.hello #=> „Hello Maciek!”
Try it out!
Ruby: An introduction – syntax
Maciej Mensfeld 9/23
Ruby syntax – arrays, hashes (dictionaries)
Arrays names = [‘Maciek’, ‘John’, ‘Freddy’]
names.length #=> 3debts.length #=> 2
Hashes debts={„Maciek”=>1, „John”=> 10}
Ruby: An introduction – syntax
Maciej Mensfeld 10/23
Ruby syntax – loops
Ruby:friends.each{|friend| puts friend }
C:for(i=0; i<number_of_elements;i++){ print element[i]}
10.times {|i| puts i }10.downto(1){|i| puts i }
There is no standard „for” loop in Ruby!
Try it out!
Ruby: An introduction – syntax
Maciej Mensfeld 11/23
Ruby craziness - symbols
OMG symbols are so weird…
When you ask someone : what are symbols in Ruby? Most programmers will say: they simple are!
A symbol in Ruby is an instance of the class Symbol. A symbol is defined by prefixing a colon with an
identifier. :name, :id, :user
Symbols are most commonly used in creating hashes:h = {:name => "Jayson", :email => „[email protected]"}
The advantage in using symbols is the efficient use of memory. Maximum space taken by a symbol is never more than the space taken by an integer. This is because internally symbol is stored as an integer. In case of strings the memory space depends on the size of the string.
Ruby: An introduction – syntax
Maciej Mensfeld 12/23
Ruby craziness - symbolsAlso whenever a string is used in the program, a new instance is created. But for symbols, same identifier points to the same memory location!
puts "name".object_idputs "name".object_idputs :name.object_idputs :name.object_id
Compare:puts "name".object_id == "name".object_idputs :name.object_id == :name.object_id
Try it out!
Ruby: writing some cool stuff
Maciej Mensfeld 23
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
Ruby: writing some cool stuff
Maciej Mensfeld 23
Web crawler – page content parser
What do we need?
Parser Crawler
Extracts data from page content Crawl all available pages
Ruby: writing some cool stuff
Maciej Mensfeld 23
Simple parser – 13LOC
attr_reader – set instance variable as readonly from the outsideattr_accessor – make instanca variable R/W from the outside
Ruby: writing some cool stuff
Maciej Mensfeld 16/23
Simple parser – 13LOC
@keyword.is_a?(String) ? @keyword.downcase : @keyword
Just like in C and PHP:
condition_true ? if true do smthng : if false do smthng
Try it out! true ? puts(„I’m true!”) : puts(„I’m false!”)
false ? puts(„I’m true!”) : puts(„I’m false!”)
Ruby: writing some cool stuff
Maciej Mensfeld 18/23
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
Ruby: writing some cool stuff
Maciej Mensfeld 19/23
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)
Ruby: 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!
Ruby: writing some cool stuff
Maciej Mensfeld 21/23
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!
Ruby: writing some cool stuff
Maciej Mensfeld 22/30
THX
Presented by:
Maciej Mensfeld
github.com/mensfeld