1 / 36

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 senior ruby developer@furioustribe.com. maciej@mensfeld.pl dev.mensfeld.pl github.com / mensfeld. Maciej Mensfeld. Ruby: An introduction – please ….

conlan
Télécharger la présentation

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 senior rubydeveloper@furioustribe.com maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld

  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

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

  4. Ruby: An introduction – Whatis Ruby? Whatis Ruby? • Ruby islike an Iron Man: • Shiny; • Red; • Sometimesquite heavy; • Powerfull; • Needselectricity; (and youcanuseitwith Lego Mindstorms) Maciej Mensfeld

  5. 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

  6. What Ruby likes? What Ruby likes? Ruby likes to talk! Maciej Mensfeld

  7. Wholikes Ruby :-) Wholikes to use Ruby? Maciej Mensfeld

  8. What Ruby is NOT? What Ruby is not? • Universal solution for lazyprogrammers; • Universal solutionin general; • Ruby is not an Iron Man ;) • Designed for smallapplications (withRailsin general); • Python; • Better PHP; • Somethingthat will work on Windows (don’teventhingaboutit!); Maciej Mensfeld

  9. Ruby: An introduction – Whatis Ruby? Ruby community Maciej Mensfeld

  10. A bit moreabout Ruby… Stayclean and nice! Maciej Mensfeld

  11. 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” Maciej Mensfeld

  12. 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

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

  14. Ruby: An introduction – syntax Ruby syntax – classes, methods, objects # 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

  15. 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

  16. Ruby: An introduction – syntax Ruby syntax – loops Ruby: friends.each{|friend| putsfriend } 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

  17. 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

  18. 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! 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

  19. Interactive Ruby Shell Interactive Ruby Shell Interactive Ruby Shell (IRB) is a shell for programming in the object-oriented scripting language Ruby. The program is launched from a command line and allows the execution of Ruby commands with immediate response, experimenting in real-time. Maciej Mensfeld

  20. Interactive Ruby Shell Fewsimpleexamples Typesomethinginto IRB and getresult of lastevaluatedexpression Typesomethinginto IRB and getresult of lastevaluatedexpression Calculate! Maciej Mensfeld

  21. Syntaxbasics Comments # Single linecomments start with a „#” # Youcanalwaysusethemlikethis :-) # So youcanhavemultiplycomment lines # Thisapproachis most common =begin This is a comment line it explains that the next line of code displays a welcome message =end Maciej Mensfeld

  22. Syntaxbasics Reservedwords BEGIN do next then END else nill true alias elsif not undef and end or unless begin ensure redo until break false rescue when case for retry while class if return while def in self __FILE__ defined? module super __LINE__ The following list shows the reserved words in Ruby. These reserved words may not be used as constant or variable names. They can, however, be used as method names. Maciej Mensfeld

  23. Syntaxbasics Variables Global variables start with $ Local variables begin with a lowercase letter or _. The scope of a local variable ranges from class, module, def, or do to the corresponding end or from a block's opening brace to its close brace {}. Therearealsoclass and instancevariables – but we will getthereinnextchapter Maciej Mensfeld

  24. Syntaxbasics Variables When an uninitialized local variable is referenced, it is interpreted as a call to a method that has no arguments. Assignment to uninitialized local variables also serves as variable declaration. The variables start to exist until the end of the current scope is reached. The lifetime of local variables is determined when Ruby parses the program. Maciej Mensfeld

  25. Syntaxbasics Pseudo-Variables They are special variables that have the appearance of local variables but behave like constants. You can not assign any value to these variables. • self: The receiver object of the current method. • true: Value representing true. • false: Value representing false. • nil: Value representing undefined. • __FILE__: The name of the current source file. • __LINE__: The current line number in the source file. Variablesarecoming… Maciej Mensfeld

  26. Syntaxbasics Conditions - if if conditional [then] code... [elsif conditional [then] code...]... [else code...] end Youcanuseif as a conditionalmodifier… Maciej Mensfeld

  27. Syntaxbasics Conditions - unless unless conditional [then] code [else code ] end Youcanuseunless as a conditionalmodifier… Maciej Mensfeld

  28. Syntaxbasics Casestatement case expression [when expression [, expression ...] [then] code ]... [else code ] end Maciej Mensfeld

  29. Syntaxbasics Loops Maciej Mensfeld

  30. Syntaxbasics Loops Maciej Mensfeld

  31. Syntaxbasics Loops „For” loopis a great idea but not in Ruby! (useitin PHP) Maciej Mensfeld

  32. Syntaxbasics Strings Maciej Mensfeld

  33. Syntaxbasics Strings Expression substitution is a means of embedding the value of any Ruby expression into a string using #{ and }: Maciej Mensfeld

  34. Syntaxbasics Numbers Maciej Mensfeld

  35. Syntaxbasics Numbers Numbersareinstances of classes! Maciej Mensfeld

  36. Ruby: An introduction THX Presented by: Maciej Mensfeld maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld

More Related