360 likes | 497 Views
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 ….
E N D
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
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
Ruby: An introduction – Whatis Ruby? Ruby WT*? Ruby pictures Maciej Mensfeld
Ruby: An introduction – Whatis Ruby? Whatis Ruby? • Ruby islike an Iron Man: • Shiny; • Red; • Sometimesquite heavy; • Powerfull; • Needselectricity; (and youcanuseitwith Lego Mindstorms) Maciej Mensfeld
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
What Ruby likes? What Ruby likes? Ruby likes to talk! Maciej Mensfeld
Wholikes Ruby :-) Wholikes to use Ruby? Maciej Mensfeld
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
Ruby: An introduction – Whatis Ruby? Ruby community Maciej Mensfeld
A bit moreabout Ruby… Stayclean and nice! Maciej Mensfeld
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
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
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
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
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
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
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
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
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
Interactive Ruby Shell Fewsimpleexamples Typesomethinginto IRB and getresult of lastevaluatedexpression Typesomethinginto IRB and getresult of lastevaluatedexpression Calculate! Maciej Mensfeld
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
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
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
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
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
Syntaxbasics Conditions - if if conditional [then] code... [elsif conditional [then] code...]... [else code...] end Youcanuseif as a conditionalmodifier… Maciej Mensfeld
Syntaxbasics Conditions - unless unless conditional [then] code [else code ] end Youcanuseunless as a conditionalmodifier… Maciej Mensfeld
Syntaxbasics Casestatement case expression [when expression [, expression ...] [then] code ]... [else code ] end Maciej Mensfeld
Syntaxbasics Loops Maciej Mensfeld
Syntaxbasics Loops Maciej Mensfeld
Syntaxbasics Loops „For” loopis a great idea but not in Ruby! (useitin PHP) Maciej Mensfeld
Syntaxbasics Strings Maciej Mensfeld
Syntaxbasics Strings Expression substitution is a means of embedding the value of any Ruby expression into a string using #{ and }: Maciej Mensfeld
Syntaxbasics Numbers Maciej Mensfeld
Syntaxbasics Numbers Numbersareinstances of classes! Maciej Mensfeld
Ruby: An introduction THX Presented by: Maciej Mensfeld maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld