36
Ruby: An introduction - Who am I? Maciej Mensfeld Presented by: Maciej Mensfeld Ruby: An introduction [email protected] dev.mensfeld.pl github.com/mensfeld senior ruby [email protected] senior ruby [email protected]

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

Embed Size (px)

Citation preview

Page 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

Ruby: An introduction - Who am I?

Maciej Mensfeld

Presented by:

Maciej Mensfeld

Ruby: An introduction

[email protected]

github.com/mensfeld

senior ruby [email protected] ruby [email protected]

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

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

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

Ruby: An introduction – What is Ruby?

Maciej Mensfeld

Ruby WT*?

Ruby pictures

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

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)

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

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;

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

What Ruby likes?

Maciej Mensfeld

What Ruby likes?

Ruby likes to talk!

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

Who likes Ruby :-)

Maciej Mensfeld

Who likes to use Ruby?

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

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!);

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

Ruby: An introduction – What is Ruby?

Maciej Mensfeld

Ruby community

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

A bit more about Ruby…

Maciej Mensfeld

Stay clean and nice!

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

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”

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

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 #=> Stringnil.class #=> NilClass1.class #=> Integer{}.class #=> Hash[].class #=> Arrayself.class #=> Object(0..9).class #=> Range

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

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!”

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

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 }!” endend

msg = Message.new(„Maciek”)msg.hello #=> „Hello Maciek!”

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

Ruby: An introduction – syntax

Maciej Mensfeld

Ruby syntax – arrays, hashes (dictionaries)

Arrays names = [‘Maciek’, ‘John’, ‘Freddy’]

names.length #=> 3debts.length #=> 2

Hashes debts={„Maciek”=>1, „John”=> 10}

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

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!

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

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 => „[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.

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

Ruby: An introduction – syntax

Maciej Mensfeld

Ruby craziness - symbolsAlso 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_idputs "name".object_idputs :name.object_idputs :name.object_id

Compare:puts "name".object_id == "name".object_idputs :name.object_id == :name.object_id

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

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.

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

Interactive Ruby Shell

Maciej Mensfeld

Few simple examples

Type something into IRB and get result of last evaluated expression

Type something into IRB and get result of last evaluated expression

Calculate!

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

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

=beginThis is a comment lineit explains that the next line of code displays a welcome message=end

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

Syntax basics

Maciej Mensfeld

Reserved words

BEGIN do next thenEND else nill truealias elsif not undefand end or unlessbegin ensure redo untilbreak false rescue whencase for retry whileclass if return whiledef 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.

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

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

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

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.

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

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…

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

Syntax basics

Maciej Mensfeld

Conditions - ifif conditional [then]

code...[elsif conditional [then]

code...]...[else

code...]end

You can use if as a conditional modifier…

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

Syntax basics

Maciej Mensfeld

Conditions - unlessunless conditional [then] code[else code ]end

You can use unless as a conditional modifier…

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

Syntax basics

Maciej Mensfeld

Case statement

case expression[when expression [, expression ...] [then] code ]...[else code ]end

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

Syntax basics

Maciej Mensfeld

Loops

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

Syntax basics

Maciej Mensfeld

Loops

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

Syntax basics

Maciej Mensfeld

Loops

„For” loop is a great idea but not in Ruby! (use it in PHP)

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

Syntax basics

Maciej Mensfeld

Strings

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

Syntax basics

Maciej Mensfeld

Strings

Expression substitution is a means of embedding the value of any Ruby expression into a string using #{ and }:

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

Syntax basics

Maciej Mensfeld

Numbers

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

Syntax basics

Maciej Mensfeld

Numbers

Numbers are instances of classes!

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

Ruby: An introduction

Maciej Mensfeld

THX

Presented by:

Maciej Mensfeld

[email protected]

github.com/mensfeld