Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ruby: An introduction - Who am I? Maciej Mensfeld Presented by: Maciej Mensfeld Ruby: An introduction dev.mensfeld.pl github.com/mensfeld.

Similar presentations


Presentation on theme: "Ruby: An introduction - Who am I? Maciej Mensfeld Presented by: Maciej Mensfeld Ruby: An introduction dev.mensfeld.pl github.com/mensfeld."— Presentation transcript:

1 Ruby: An introduction - Who am I? Maciej Mensfeld Presented by: Maciej Mensfeld Ruby: An introduction maciej@mensfeld.pl dev.mensfeld.pl github.com/mensfeld senior ruby developer@araneo.pl senior ruby developer@furioustribe.com

2 Ruby: An introduction – please… Maciej Mensfeld 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

3 Ruby: An introduction – What is Ruby? Maciej Mensfeld Ruby WT*? Ruby pictures

4 Ruby: An introduction – What is Ruby? Maciej Mensfeld 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)

5 Ruby: An introduction – What is Ruby? Maciej Mensfeld 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;

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

7 Who likes Ruby :-) Maciej Mensfeld Who likes to use Ruby?

8 What Ruby is NOT? Maciej Mensfeld 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!);

9 Ruby: An introduction – What is Ruby? Maciej Mensfeld Ruby community

10 A bit more about Ruby… Maciej Mensfeld Stay clean and nice!

11 Ruby: An introduction – What I love in Ruby? Maciej Mensfeld Clarity not ceremony – Main program Java: public class HelloWorld{ public static void main(String args){ System.out.println(„Hello World”); } Ruby: puts „Hello World”

12 Ruby: An introduction – What I love in Ruby? Maciej Mensfeld 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

13 Ruby: An introduction – syntax Maciej Mensfeld 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!”

14 Ruby: An introduction – syntax Maciej Mensfeld 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 msg = Message.new(„Maciek”) msg.hello #=> „Hello Maciek!”

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

16 Ruby: An introduction – syntax Maciej Mensfeld 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!

17 Ruby: An introduction – syntax Maciej Mensfeld 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 => „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.

18 Ruby: An introduction – syntax Maciej Mensfeld 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 Compare: puts "name".object_id == "name".object_id puts :name.object_id == :name.object_id

19 Interactive Ruby Shell Maciej Mensfeld 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.

20 Interactive Ruby Shell Maciej Mensfeld Few simple examples Type something into IRB and get result of last evaluated expression Calculate!

21 Syntax basics Maciej Mensfeld 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

22 Syntax basics Maciej Mensfeld Reserved words BEGINdonextthen ENDelsenilltrue aliaselsifnotundef andendorunless beginensureredountil breakfalserescuewhen caseforretrywhile classifreturnwhile definself__FILE__ defined?modulesuper__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.

23 Syntax basics Maciej Mensfeld 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

24 Syntax basics Maciej Mensfeld 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.

25 Syntax basics Maciej Mensfeld 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. Variables are coming…

26 Syntax basics Maciej Mensfeld Conditions - if if conditional [then] code... [elsif conditional [then] code...]... [else code...] end You can use if as a conditional modifier…

27 Syntax basics Maciej Mensfeld Conditions - unless unless conditional [then] code [else code ] end You can use unless as a conditional modifier…

28 Syntax basics Maciej Mensfeld Case statement case expression [when expression [, expression...] [then] code ]... [else code ] end

29 Syntax basics Maciej Mensfeld Loops

30 Syntax basics Maciej Mensfeld Loops

31 Syntax basics Maciej Mensfeld Loops „For” loop is a great idea but not in Ruby! (use it in PHP)

32 Syntax basics Maciej Mensfeld Strings

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

34 Syntax basics Maciej Mensfeld Numbers

35 Syntax basics Maciej Mensfeld Numbers Numbers are instances of classes!

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


Download ppt "Ruby: An introduction - Who am I? Maciej Mensfeld Presented by: Maciej Mensfeld Ruby: An introduction dev.mensfeld.pl github.com/mensfeld."

Similar presentations


Ads by Google