[ “Love”, :Ruby ] .each { |i| p i } Herval Freire, march 2007
What is Ruby? The Pearl is the June’s month gem Ruby was first released on July 95 by a japanese nut (Matz).  Ruby == Perl++! (but without the ++ operator…)
What is Ruby? An 100% OO language (well, 99.9%) One of a few languages  not designed by a committee! Strongly, Implicitly, Dynamically typed language Open Source (GPL) with implementations on Java VM (JRuby), Windows, several Linux distributions, BeOS,.Net… Concise. Simple. Fun!
Hello, World! public class HelloWorld { public static void main(String[] args) { for(int i = 0; i < 10; i++) { System.out.println(i + “ times…”); } } }
Hello, World! 10.times do |word| puts “#{word} times…” end
Conventions CamelCaseClassNames @instance_variables @@class_variables $globals CONSTANTS ConstantsToo (yes, classes are constants) :symbols Not mandatory, but recommended… dasherized_class_files.rb dasherized_methods_and_variables boolean_methods? destructive_methods!
Wacky Syntax… You don’t need ; to end lines, but you can. Parenthesis are optional. But not always. There’s no begin without an end. But things can end without a begin { } or begin/end. Whatever. If  something  then… well, not necessarily  then .
Control Expressions if a == true then puts “yes” end if a == true { puts “yes” } puts “yes” if a == true if a == true puts “ok, you got the point…” end
And more control expressions… if...elsif...else...end unless (same as “if not”) while (while expr do … end) loop (loop do … end) for (for x in y do … end) case (case x when y … else … end)
Boolean operations !, ||, && - same as Java. Evaluation from left to right not, and, or – evaluation from right to left
Implicit typing a = “some string” a.class “ String” 1.class Fixnum
No Interfaces! “ Duck Typing” philosophy: If it walks like a duck, And talks like a duck, Then we can treat it like a duck. (who cares what it really is)
No Interfaces! class Carro def faz_barulho puts “vrummm” end end class Aviao def faz_barulho puts “vloshhhhhhhh” end end mazda = Carro.new mazda.respond_to?(:faz_barulho) True
And no NullPointers, too Nil is 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 def initialize(owner_name, purchase_date = Time::now) @owner = owner_name @purchase_date = purchase_date end end yamaha = Motocicleta.new( “John Doe”) puts yamaha.owner “ John Doe” puts yamaha.instance_variables [@owner, @purchase_date] puts Yahama.purchase_date NoMethodError
Getters and Setters, be gone! Every attribute 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” car, parts = “Peugeot”, [:engine, :wheels, :seats] puts parts.size 2 put parts.class Array
Yield that block, will ya’? def delegate_something(&block) block.call(1, 2, 3) end delegate_something { |a, b, c| puts a, b, c } 1 2 3 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 “ JRuby is tightly integrated with Java to allow the embedding of the interpreter into any Java application with full two-way access between the Java and the Ruby code. (Compare Jython for the Python language.) “
Conclusion A very flexible and natural language Based on few, but powerful concepts: closures, blocks, messages Not a tyranical language: bad programmers can produce really nasty code with little or no restrictions...

[ “Love", :Ruby ].each { |i| p i }

  • 1.
    [ “Love”, :Ruby] .each { |i| p i } Herval Freire, march 2007
  • 2.
    What is Ruby?The Pearl is the June’s month gem Ruby was first released on July 95 by a japanese nut (Matz). Ruby == Perl++! (but without the ++ operator…)
  • 3.
    What is Ruby?An 100% OO language (well, 99.9%) One of a few languages not designed by a committee! Strongly, Implicitly, Dynamically typed language Open Source (GPL) with implementations on Java VM (JRuby), Windows, several Linux distributions, BeOS,.Net… Concise. Simple. Fun!
  • 4.
    Hello, World! publicclass HelloWorld { public static void main(String[] args) { for(int i = 0; i < 10; i++) { System.out.println(i + “ times…”); } } }
  • 5.
    Hello, World! 10.timesdo |word| puts “#{word} times…” end
  • 6.
    Conventions CamelCaseClassNames @instance_variables@@class_variables $globals CONSTANTS ConstantsToo (yes, classes are constants) :symbols Not mandatory, but recommended… dasherized_class_files.rb dasherized_methods_and_variables boolean_methods? destructive_methods!
  • 7.
    Wacky Syntax… Youdon’t need ; to end lines, but you can. Parenthesis are optional. But not always. There’s no begin without an end. But things can end without a begin { } or begin/end. Whatever. If something then… well, not necessarily then .
  • 8.
    Control Expressions ifa == true then puts “yes” end if a == true { puts “yes” } puts “yes” if a == true if a == true puts “ok, you got the point…” end
  • 9.
    And more controlexpressions… if...elsif...else...end unless (same as “if not”) while (while expr do … end) loop (loop do … end) for (for x in y do … end) case (case x when y … else … end)
  • 10.
    Boolean operations !,||, && - same as Java. Evaluation from left to right not, and, or – evaluation from right to left
  • 11.
    Implicit typing a= “some string” a.class “ String” 1.class Fixnum
  • 12.
    No Interfaces! “Duck Typing” philosophy: If it walks like a duck, And talks like a duck, Then we can treat it like a duck. (who cares what it really is)
  • 13.
    No Interfaces! classCarro def faz_barulho puts “vrummm” end end class Aviao def faz_barulho puts “vloshhhhhhhh” end end mazda = Carro.new mazda.respond_to?(:faz_barulho) True
  • 14.
    And no NullPointers,too Nil is an object puts nil.class NilClass a = nil a.nil? True
  • 15.
    Classes, Structs andModules 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.
  • 16.
    Classes: those promiscuousbastards… 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…”
  • 17.
    But wait, there’smore! 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
  • 18.
    Yes, we havereflection! puts palio.methods [“methods”, …, “faz_barulho”] palio.send(“faz_barulho”) “ vrum vrum”
  • 19.
    Attributes and Constructorsclass Motocicleta attr_accessor :owner def initialize(owner_name, purchase_date = Time::now) @owner = owner_name @purchase_date = purchase_date end end yamaha = Motocicleta.new( “John Doe”) puts yamaha.owner “ John Doe” puts yamaha.instance_variables [@owner, @purchase_date] puts Yahama.purchase_date NoMethodError
  • 20.
    Getters and Setters,be gone! Every attribute 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
  • 21.
    The tale ofthe 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…”
  • 22.
    /regexp/ Same syntaxas Perl Matching operator: =~ /joao/.match(“joao”) == “joao” =~ /joao/ == “joao”.match(/joao/)
  • 23.
    Catch that! begin… rescue [error_type [=> variable]] ensure end
  • 24.
    Useful classes, strangesyntaxes Hashes Hash.new… or { } hash = Hash.new hash.store “key”, “value” puts hash.get(“key”) Or… hash = {} hash[“key”] = “value” puts hash[“key”]
  • 25.
    Useful classes, strangesyntaxes Arrays Array.new… or [] a = Array.new a[4] = “a” [nil, nil, nil, “a”] Or… a = [] a << “a” [“a”]
  • 26.
    Useful classes, strangesyntaxes 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’”
  • 27.
    :symbols A symbolis 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”
  • 28.
    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
  • 29.
    Did I sayblock assignment? nome, endereco, telefone = “John Doe”, “Gotham City”, “+1 123 456 789” puts nome “ John Doe” car, parts = “Peugeot”, [:engine, :wheels, :seats] puts parts.size 2 put parts.class Array
  • 30.
    Yield that block,will ya’? def delegate_something(&block) block.call(1, 2, 3) end delegate_something { |a, b, c| puts a, b, c } 1 2 3 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”!
  • 31.
    JRuby! A ruby1.8 implementation on the Java VM Currently on v0.92 “ JRuby is tightly integrated with Java to allow the embedding of the interpreter into any Java application with full two-way access between the Java and the Ruby code. (Compare Jython for the Python language.) “
  • 32.
    Conclusion A veryflexible and natural language Based on few, but powerful concepts: closures, blocks, messages Not a tyranical language: bad programmers can produce really nasty code with little or no restrictions...