("Love",:Ruby) .Each ( - I - P I)
("Love",:Ruby) .Each ( - I - P I)
.each { |i| p i }
10.times do |word|
puts “#{word} times…”
end
Conventions
CamelCaseClassNames
@instance_variables
@@class_variables
$globals
CONSTANTS
ConstantsToo (yes, classes are constants)
:symbols
1.class
⇒ Fixnum
No Interfaces!
“Duck Typing” philosophy:
class Aviao
def faz_barulho
puts “vloshhhhhhhh”
end
end
mazda = Carro.new
mazda.respond_to?(:faz_barulho)
⇒ True
And no NullPointers, too
Nilis an object
puts nil.class
⇒ NilClass
a = nil
a.nil?
⇒ True
Classes, Structs and
Modules
A Class is an extensible definition of
something (final classes will only be
available on ruby 1.9).
A Module is the definition of a class. It
cannot be instantiated, nor extended. But
classes can include it, or extend it
(mixins).
A Struct is a (generally) temporary class
made of something. Or a bunch of some
things.
Classes: those promiscuous
bastards…
class Carro << SomethingElse
def faz_barulho
puts “vruumm”
end
end
palio = Carro.new
puts palio.faz_barulho
⇒ “vruumm”
class Carro
def faz_barulho
puts “cof cof cof…”
end
End
puts palio.faz_barulho
⇒ “cof cof cof…”
But wait, there’s more!
module CarThingie
def faz_barulho
“vrum vrum”
end
end
palio.extend(CarThingie)
puts palio.faz_barulho
⇒ “vrum vrum”
Or in other words:
class Carro
include CarThingie
end
puts palio.is_a?(CarThingie)
⇒ true
Yes, we have reflection!
puts palio.methods
⇒ [“methods”, …, “faz_barulho”]
palio.send(“faz_barulho”)
⇒ “vrum vrum”
Attributes and Constructors
class Motocicleta
attr_accessor :owner
puts yamaha.instance_variables
⇒ [@owner, @purchase_date]
puts Yahama.purchase_date
⇒ NoMethodError
Getters and Setters, be
gone!
Everyattribute is automatically
encapsulated, but overriding is easy:
class Motocicleta
def owner=new_owner
# you can do something else if you want…
@owner = new_owner
end
end
The tale of the missing
method
class Carro
def method_missing(method, *args,
&block)
puts “Ops! Something wrong was
called…”
end
end
mazda.some_unexisting_crap
⇒ “Ops! Something wrong was called…”
/regexp/
Same syntax as Perl
Matching operator: =~
/joao/.match(“joao”)
== “joao” =~
/joao/ == “joao”.match(/joao/)
Catch that!
begin
…
rescue [error_type [=> variable]]
ensure
end
Useful classes, strange
syntaxes
Hashes
– Hash.new… or { }
hash = Hash.new
hash.store “key”, “value”
puts hash.get(“key”)
Or…
hash = {}
hash[“key”] = “value”
puts hash[“key”]
Useful classes, strange
syntaxes
Arrays
– Array.new… or []
a = Array.new
a[4] = “a”
⇒ [nil, nil, nil, “a”]
Or…
a = []
a << “a”
[“a”]
Useful classes, strange
syntaxes
Strings
– String.new or “”
a = “some string”
puts a[3]
⇒ “e”
puts a[0..3]
⇒ “some”
puts a[0…3]
⇒ “som”
puts a[-10..-7]
⇒ “some”
a[“string”] = “ruby lovin’”
⇒ “some ruby lovin’”
:symbols
A symbol is something that is not yet
anything, but it is already there. Like
a string, without the string part.
Constants, keys on maps, string
representations.
john = { :name => “John Doe”, :car
=> “Palio” }
puts john[:name]
⇒ “John Doe”
Ah… Blocks.
something = 10, something_else = 20
my_price = {
if something == 20
1
elsif something_else = 10
2
else
3
end
}
puts my_price
⇒ 3
Did I say block assignment?
nome, endereco, telefone = “John Doe”,
“Gotham City”, “+1 123 456 789”
puts nome
⇒ “John Doe”
def delegate_with_yield(a, b, c)
puts “doing something…”
yield (a, b, c)
End
delegate_with_yield(1, 2, 3) do | a, b, c |
puts “#{a} #{b} #{c}”
end
⇒ “doing something…”
⇒ “1 2 3”!
JRuby!
A ruby 1.8 implementation on the
Java VM
– Currently on v0.92