1 / 23

Ruby: An introduction - Who am I?

Ruby: An introduction - Who am I?. Ruby: An introduction . Presented b y :. Maciej Mensfeld. senior ruby developer@araneo.pl lead ruby developer@wordwatch.com. maciej@mensfeld.pl dev.mensfeld.pl github.com / mensfeld. Maciej Mensfeld. 1/23. Ruby: An introduction – please ….

opal
Download Presentation

Ruby: An introduction - Who am I?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby: An introduction - Whoam I? Ruby: An introduction Presented by: Maciej Mensfeld senior rubydeveloper@araneo.pl leadruby developer@wordwatch.com maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld 1/23

  2. Ruby: An introduction – please… Ruby: An introduction Please… • …ask me to slow down, if I speak to quickly; • …ask me again, if I forget; • …askquestions, ifanything i sayis not clear; • …feelfree to shareyourownobservations Maciej Mensfeld 2/23

  3. Ruby: An introduction – Whatis Ruby? Ruby WT*? Ruby pictures Maciej Mensfeld 3/23

  4. Ruby: An introduction – Whatis Ruby? Whatis Ruby? • Pure object-orientedprogramminglanguage (even the number 1 is an instance of class); • Created by Yukihiro Matsumoto in 1993; • Freelyavailable 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; • Reallyhelpful community; Maciej Mensfeld 4/23

  5. Ruby: An introduction – What I lovein Ruby? Clarity not ceremony – Main program Java: public classHelloWorld{ public staticvoidmain(Stringargs){ System.out.println(„HelloWorld”); } } Ruby: puts „HelloWorld” Tryit out! Maciej Mensfeld 5/23

  6. Ruby: An introduction – What I lovein Ruby? Expressivesyntax&& objects, objects, objects… 3.times { puts „Ruby iscool”} [„Maciek”, „John”, „Anna”].first #=> „Maciek” [„Maciek”, „John”, „Anna”].last #=> „Anna” attr_accessor :name „Anna”.class #=> String nil.class #=> NilClass 1.class #=> Integer {}.class #=> Hash [].class #=> Array self.class #=> Object (0..9).class #=> Range Maciej Mensfeld 6/23

  7. Ruby: An introduction – syntax Ruby syntax – helloworld as a function def h puts „HelloWorld!” end HelloWorld! puts „HelloWorld!” Tryit out! h => „HelloWorld!” def h(name=„World”) puts „Hello #{name}!” end HelloYourName! puts „Hello #{name}” h („Maciek”)=> „Hello Maciek!” Maciej Mensfeld 7/23

  8. Ruby: An introduction – syntax Ruby syntax – classes, methods, objects Tryit out! # Commentsstartswith „#” class Messenger definitialize(name) # instancevariablesstartswith „@” @name = name end public defhello puts „Hello #{@name }!” end end HelloYourName! as an object msg = Message.new(„Maciek”) msg.hello #=> „Hello Maciek!” Maciej Mensfeld 8/23

  9. Ruby: An introduction – syntax Ruby syntax – arrays, hashes (dictionaries) Arrays names = [‘Maciek’, ‘John’, ‘Freddy’] names.length #=> 3 debts.length #=> 2 Hashes debts={„Maciek”=>1, „John”=> 10} Maciej Mensfeld 9/23

  10. Ruby: An introduction – syntax Ruby syntax – loops Ruby: friends.each{|friend| putsfriend } Tryit out! C: for(i=0; i<number_of_elements;i++) { print element[i] } 10.times {|i| puts i } 10.downto(1){|i| puts i } Thereis no standard „for” loopin Ruby! Maciej Mensfeld 10/23

  11. Ruby: An introduction – syntax Ruby craziness - symbols Whenyouasksomeone : whataresymbolsin Ruby? Most programmers will say: theysimpleare! 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 OMG symbolsare so weird… Symbols are most commonly used in creating hashes: h = {:name => "Jayson", :email => „test@gmail.com"} 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. Maciej Mensfeld 11/23

  12. Ruby: An introduction – syntax Ruby craziness - symbols Also whenever a string is used in the program, a new instance is created. But for symbols, same identifier points to the same memory location! Tryit out! puts "name".object_id puts "name".object_id puts :name.object_id puts :name.object_id Compare: puts"name".object_id == "name".object_id puts :name.object_id == :name.object_id Maciej Mensfeld 12/23

  13. Ruby: writingsomecoolstuff Web crawler! Enoughtheory! Let’s be pragmatic! Fetch and storeurls Simple webcrawler requirements Search 4 keywords (supportregexp) Don’trevisiturls Printresults Maciej Mensfeld 23

  14. Ruby: writingsomecoolstuff Web crawler – pagecontentparser What do we need? Parser Crawler Extracts data frompagecontent Crawl allavailablepages Maciej Mensfeld 23

  15. Ruby: writingsomecoolstuff Simple parser – 13LOC attr_reader – set instancevariable as readonlyfromtheoutside attr_accessor – make instancavariable R/W fromtheoutside Maciej Mensfeld 23

  16. Ruby: writingsomecoolstuff Simple parser – 13LOC @keyword.is_a?(String) ? @keyword.downcase : @keyword Just likein C and PHP: condition_true ? iftrue do smthng : iffalse do smthng Tryit out! true ? puts(„I’mtrue!”) : puts(„I’mfalse!”) false ? puts(„I’mtrue!”) : puts(„I’mfalse!”) Maciej Mensfeld 16/23

  17. Ruby: writingsomecoolstuff Simple parser – 13LOC Tryit out! Maciej Mensfeld 17/23

  18. Ruby: writingsomecoolstuff Crawler – Howshoulditwork? Try to downloadpage Success? Selectanotherpage No Yes Do somethingwithresult (parse, send, etc) Mark page as visited Maciej Mensfeld 18/23

  19. Ruby: 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 19/23

  20. Ruby: 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

  21. Ruby: 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 21/23

  22. Ruby: writingsomecoolstuff Crawler – run crawler! Tryit out! Maciej Mensfeld 22/23

  23. Ruby: writingsomecoolstuff THX Presented by: Maciej Mensfeld maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld 22/30

More Related