Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Ruby: An introduction - Who am I? Ruby: An introduction Presented by: Maciej Mensfeld senior ruby [email protected] senior ruby [email protected] [email protected] 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; • …ask questions, if anything i say is not clear; • …feel free to share your own observations Maciej Mensfeld Ruby: An introduction – What is Ruby? Ruby WT*? Ruby pictures Maciej Mensfeld Ruby: An introduction – What is Ruby? What is Ruby? Ruby is like an Iron Man: • Shiny; • Red; •Sometimes quite heavy; •Powerfull; •Needs electricity; (and you can use it with Lego Mindstorms) Maciej Mensfeld Ruby: An introduction – What is Ruby? 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; Maciej Mensfeld What Ruby likes? What Ruby likes? Ruby likes to talk! Maciej Mensfeld Who likes Ruby :-) Who likes to use Ruby? Maciej Mensfeld What Ruby is NOT? What Ruby is not? • • • • • • • Universal solution for lazy programmers; Universal solution in general; Ruby is not an Iron Man ;) Designed for small applications (with Rails in general); Python; Better PHP; Something that will work on Windows (don’t even thing about it!); Maciej Mensfeld Ruby: An introduction – What is Ruby? Ruby community Maciej Mensfeld A bit more about Ruby… Stay clean and nice! Maciej Mensfeld Ruby: An introduction – What I love in Ruby? Clarity not ceremony – Main program Java: public class HelloWorld{ public static void main(String args){ System.out.println(„Hello World”); } } Ruby: puts „Hello World” Maciej Mensfeld Ruby: An introduction – What I love in Ruby? 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 #=> 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 – 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!” Maciej Mensfeld Ruby: An introduction – syntax 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 }!” end end 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 Maciej Mensfeld debts={„Maciek”=>1, „John”=> 10} Ruby: An introduction – syntax 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! Maciej Mensfeld Ruby: An introduction – syntax Ruby craziness - symbols 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. Maciej Mensfeld OMG symbols are so weird… 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 Few simple examples Type something into IRB and get result of last evaluated expression Calculate! Maciej Mensfeld Type something into IRB and get result of last evaluated expression Syntax basics Comments # Single line comments start with a „#” # You can always use them like this :-) # So you can have multiply comment lines # This approach is most common =begin This is a comment line it explains that the next line of code displays a welcome message =end Maciej Mensfeld Syntax basics Reserved words BEGIN END alias and begin break case class def defined? do else elsif end ensure false for if in module next nill not or redo rescue retry return self super then true undef unless until when while while __FILE__ __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 Syntax basics 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 {}. There are also class and instance variables – but we will get there in next chapter Maciej Mensfeld Syntax basics 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 Syntax basics 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. Maciej Mensfeld Variables are coming… Syntax basics Conditions - if if conditional [then] code... [elsif conditional [then] code...]... [else code...] end You can use if as a conditional modifier… Maciej Mensfeld Syntax basics Conditions - unless unless conditional [then] code [else code ] end You can use unless as a conditional modifier… Maciej Mensfeld Syntax basics Case statement case expression [when expression [, expression ...] [then] code ]... [else code ] end Maciej Mensfeld Syntax basics Loops Maciej Mensfeld Syntax basics Loops Maciej Mensfeld Syntax basics Loops „For” loop is a great idea but not in Ruby! (use it in PHP) Maciej Mensfeld Syntax basics Strings Maciej Mensfeld Syntax basics Strings Expression substitution is a means of embedding the value of any Ruby expression into a string using #{ and }: Maciej Mensfeld Syntax basics Numbers Maciej Mensfeld Syntax basics Numbers Numbers are instances of classes! Maciej Mensfeld Ruby: An introduction THX Presented by: Maciej Mensfeld [email protected] dev.mensfeld.pl github.com/mensfeld Maciej Mensfeld