Ruby: An introduction - Who am I? Maciej Mensfeld Presented by: Maciej Mensfeld Ruby: An...

Preview:

Citation preview

Ruby: An introduction - Who am I?

Maciej Mensfeld

Presented by:

Maciej Mensfeld

Ruby: An introduction

maciej@mensfeld.pldev.mensfeld.pl

github.com/mensfeld

senior ruby developer@araneo.plsenior ruby developer@furioustribe.com

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

Ruby: An introduction – What is Ruby?

Maciej Mensfeld

Ruby WT*?

Ruby pictures

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)

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;

What Ruby likes?

Maciej Mensfeld

What Ruby likes?

Ruby likes to talk!

Who likes Ruby :-)

Maciej Mensfeld

Who likes to use Ruby?

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

Ruby: An introduction – What is Ruby?

Maciej Mensfeld

Ruby community

A bit more about Ruby…

Maciej Mensfeld

Stay clean and nice!

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”

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

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

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

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}

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!

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.

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

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.

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!

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

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.

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

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.

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…

Syntax basics

Maciej Mensfeld

Conditions - ifif conditional [then]

code...[elsif conditional [then]

code...]...[else

code...]end

You can use if as a conditional modifier…

Syntax basics

Maciej Mensfeld

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

You can use unless as a conditional modifier…

Syntax basics

Maciej Mensfeld

Case statement

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

Syntax basics

Maciej Mensfeld

Loops

Syntax basics

Maciej Mensfeld

Loops

Syntax basics

Maciej Mensfeld

Loops

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

Syntax basics

Maciej Mensfeld

Strings

Syntax basics

Maciej Mensfeld

Strings

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

Syntax basics

Maciej Mensfeld

Numbers

Syntax basics

Maciej Mensfeld

Numbers

Numbers are instances of classes!

Ruby: An introduction

Maciej Mensfeld

THX

Presented by:

Maciej Mensfeld

maciej@mensfeld.pldev.mensfeld.pl

github.com/mensfeld

Recommended