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

4,300 views

Published on

Introdutory presentation on Ruby

Published in: Technology, Education
0 Comments
11 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,300
On SlideShare
0
From Embeds
0
Number of Embeds
73
Actions
Shares
0
Downloads
0
Comments
0
Likes
11
Embeds 0
No embeds

No notes for slide

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

  1. [ “Love”, :Ruby ] .each { |i| p i } Herval Freire, march 2007
  2. What is Ruby? <ul><li>The Pearl is the June’s month gem </li></ul><ul><li>Ruby was first released on July 95 by a japanese nut (Matz). </li></ul><ul><li>Ruby == Perl++! </li></ul><ul><li>(but without the ++ operator…) </li></ul>
  3. What is Ruby? <ul><li>An 100% OO language (well, 99.9%) </li></ul><ul><li>One of a few languages not designed by a committee! </li></ul><ul><li>Strongly, Implicitly, Dynamically typed language </li></ul><ul><li>Open Source (GPL) with implementations on Java VM (JRuby), Windows, several Linux distributions, BeOS,.Net… </li></ul><ul><li>Concise. Simple. Fun! </li></ul>
  4. Hello, World! <ul><li>public class HelloWorld { </li></ul><ul><li>public static void main(String[] args) { </li></ul><ul><li>for(int i = 0; i < 10; i++) { </li></ul><ul><li>System.out.println(i + “ times…”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  5. Hello, World! <ul><ul><ul><li>10.times do |word| </li></ul></ul></ul><ul><ul><ul><li>puts “#{word} times…” </li></ul></ul></ul><ul><ul><ul><li>end </li></ul></ul></ul>
  6. Conventions <ul><li>CamelCaseClassNames </li></ul><ul><li>@instance_variables </li></ul><ul><li>@@class_variables </li></ul><ul><li>$globals </li></ul><ul><li>CONSTANTS </li></ul><ul><li>ConstantsToo (yes, classes are constants) </li></ul><ul><li>:symbols </li></ul><ul><li>Not mandatory, but recommended… </li></ul><ul><li>dasherized_class_files.rb </li></ul><ul><li>dasherized_methods_and_variables </li></ul><ul><li>boolean_methods? </li></ul><ul><li>destructive_methods! </li></ul>
  7. Wacky Syntax… <ul><li>You don’t need ; to end lines, but you can. </li></ul><ul><li>Parenthesis are optional. But not always. </li></ul><ul><li>There’s no begin without an end. But things can end without a begin </li></ul><ul><li>{ } or begin/end. Whatever. </li></ul><ul><li>If something then… well, not necessarily then . </li></ul>
  8. Control Expressions <ul><li>if a == true then puts “yes” end </li></ul><ul><li>if a == true { puts “yes” } </li></ul><ul><li>puts “yes” if a == true </li></ul><ul><li>if a == true </li></ul><ul><li>puts “ok, you got the point…” </li></ul><ul><li>end </li></ul>
  9. And more control expressions… <ul><li>if...elsif...else...end </li></ul><ul><li>unless (same as “if not”) </li></ul><ul><li>while (while expr do … end) </li></ul><ul><li>loop (loop do … end) </li></ul><ul><li>for (for x in y do … end) </li></ul><ul><li>case (case x when y … else … end) </li></ul>
  10. Boolean operations <ul><li>!, ||, && - same as Java. Evaluation from left to right </li></ul><ul><li>not, and, or – evaluation from right to left </li></ul>
  11. Implicit typing <ul><li>a = “some string” </li></ul><ul><li>a.class </li></ul><ul><li>“ String” </li></ul><ul><li>1.class </li></ul><ul><li>Fixnum </li></ul>
  12. No Interfaces! <ul><li>“ Duck Typing” philosophy: </li></ul><ul><ul><ul><li>If it walks like a duck, </li></ul></ul></ul><ul><ul><ul><li>And talks like a duck, </li></ul></ul></ul><ul><ul><ul><li>Then we can treat it like a duck. </li></ul></ul></ul><ul><ul><ul><li>(who cares what it really is) </li></ul></ul></ul>
  13. No Interfaces! <ul><li>class Carro </li></ul><ul><li>def faz_barulho </li></ul><ul><li>puts “vrummm” </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>class Aviao </li></ul><ul><li>def faz_barulho </li></ul><ul><li>puts “vloshhhhhhhh” </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>mazda = Carro.new </li></ul><ul><li>mazda.respond_to?(:faz_barulho) </li></ul><ul><li>True </li></ul>
  14. And no NullPointers, too <ul><li>Nil is an object </li></ul><ul><li>puts nil.class </li></ul><ul><li>NilClass </li></ul><ul><li>a = nil </li></ul><ul><li>a.nil? </li></ul><ul><li>True </li></ul>
  15. Classes, Structs and Modules <ul><li>A Class is an extensible definition of something (final classes will only be available on ruby 1.9). </li></ul><ul><li>A Module is the definition of a class. It cannot be instantiated, nor extended. But classes can include it, or extend it (mixins). </li></ul><ul><li>A Struct is a (generally) temporary class made of something. Or a bunch of some things. </li></ul>
  16. Classes: those promiscuous bastards… <ul><li>class Carro << SomethingElse </li></ul><ul><li>def faz_barulho </li></ul><ul><li>puts “vruumm” </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>palio = Carro.new </li></ul><ul><li>puts palio.faz_barulho </li></ul><ul><li>“ vruumm” </li></ul><ul><li>class Carro </li></ul><ul><li>def faz_barulho </li></ul><ul><li>puts “cof cof cof…” </li></ul><ul><li>end </li></ul><ul><li>End </li></ul><ul><li>puts palio.faz_barulho </li></ul><ul><li>“ cof cof cof…” </li></ul>
  17. But wait, there’s more! <ul><li>module CarThingie </li></ul><ul><li>def faz_barulho </li></ul><ul><li>“ vrum vrum” </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>palio.extend(CarThingie) </li></ul><ul><li>puts palio.faz_barulho </li></ul><ul><li>“ vrum vrum” </li></ul><ul><li>Or in other words: </li></ul><ul><li>class Carro </li></ul><ul><li>include CarThingie </li></ul><ul><li>end </li></ul><ul><li>puts palio.is_a?(CarThingie) </li></ul><ul><li>true </li></ul>
  18. Yes, we have reflection! <ul><li>puts palio.methods </li></ul><ul><li>[“methods”, …, “faz_barulho”] </li></ul><ul><li>palio.send(“faz_barulho”) </li></ul><ul><li>“ vrum vrum” </li></ul>
  19. Attributes and Constructors <ul><li>class Motocicleta </li></ul><ul><li>attr_accessor :owner </li></ul><ul><li>def initialize(owner_name, purchase_date = Time::now) </li></ul><ul><li>@owner = owner_name </li></ul><ul><li>@purchase_date = purchase_date </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>yamaha = Motocicleta.new( “John Doe”) </li></ul><ul><li>puts yamaha.owner </li></ul><ul><li>“ John Doe” </li></ul><ul><li>puts yamaha.instance_variables </li></ul><ul><li>[@owner, @purchase_date] </li></ul><ul><li>puts Yahama.purchase_date </li></ul><ul><li>NoMethodError </li></ul>
  20. Getters and Setters, be gone! <ul><li>Every attribute is automatically encapsulated, but overriding is easy: </li></ul><ul><ul><ul><li>class Motocicleta </li></ul></ul></ul><ul><ul><ul><li>def owner=new_owner </li></ul></ul></ul><ul><ul><ul><li># you can do something else if you want… </li></ul></ul></ul><ul><ul><ul><li>@owner = new_owner </li></ul></ul></ul><ul><ul><ul><li>end </li></ul></ul></ul><ul><ul><ul><li>end </li></ul></ul></ul>
  21. The tale of the missing method <ul><li>class Carro </li></ul><ul><li>def method_missing(method, *args, &block) </li></ul><ul><li>puts “Ops! Something wrong was called…” </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li>mazda.some_unexisting_crap </li></ul><ul><li>“ Ops! Something wrong was called…” </li></ul>
  22. /regexp/ <ul><li>Same syntax as Perl </li></ul><ul><li>Matching operator: =~ </li></ul><ul><li>/joao/.match(“joao”) == “joao” =~ /joao/ == “joao”.match(/joao/) </li></ul>
  23. Catch that! <ul><li>begin </li></ul><ul><li>… </li></ul><ul><li>rescue [error_type [=> variable]] </li></ul><ul><li>ensure </li></ul><ul><li>end </li></ul>
  24. Useful classes, strange syntaxes <ul><li>Hashes </li></ul><ul><ul><li>Hash.new… or { } </li></ul></ul><ul><li>hash = Hash.new </li></ul><ul><li>hash.store “key”, “value” </li></ul><ul><li>puts hash.get(“key”) </li></ul><ul><li>Or… </li></ul><ul><li>hash = {} </li></ul><ul><li>hash[“key”] = “value” </li></ul><ul><li>puts hash[“key”] </li></ul>
  25. Useful classes, strange syntaxes <ul><li>Arrays </li></ul><ul><ul><li>Array.new… or [] </li></ul></ul><ul><li>a = Array.new </li></ul><ul><li>a[4] = “a” </li></ul><ul><li>[nil, nil, nil, “a”] </li></ul><ul><li>Or… </li></ul><ul><li>a = [] </li></ul><ul><li>a << “a” </li></ul><ul><li>[“a”] </li></ul>
  26. Useful classes, strange syntaxes <ul><li>Strings </li></ul><ul><ul><li>String.new or “” </li></ul></ul><ul><li>a = “some string” </li></ul><ul><li>puts a[3] </li></ul><ul><li>“ e” </li></ul><ul><li>puts a[0..3] </li></ul><ul><li>“ some” </li></ul><ul><li>puts a[0…3] </li></ul><ul><li>“ som” </li></ul><ul><li>puts a[-10..-7] </li></ul><ul><li>“ some” </li></ul><ul><li>a[“string”] = “ruby lovin’” </li></ul><ul><li>“ some ruby lovin’” </li></ul>
  27. :symbols <ul><li>A symbol is something that is not yet anything, but it is already there. Like a string, without the string part. </li></ul><ul><li>Constants, keys on maps, string representations. </li></ul><ul><li>john = { :name => “John Doe”, :car => “Palio” } </li></ul><ul><li>puts john[:name] </li></ul><ul><li>“ John Doe” </li></ul>
  28. Ah… Blocks. <ul><li>something = 10, something_else = 20 </li></ul><ul><li>my_price = { </li></ul><ul><li>if something == 20 </li></ul><ul><li>1 </li></ul><ul><li>elsif something_else = 10 </li></ul><ul><li>2 </li></ul><ul><li>else </li></ul><ul><li>3 </li></ul><ul><li>end </li></ul><ul><li>} </li></ul><ul><li>puts my_price </li></ul><ul><li>3 </li></ul>
  29. Did I say block assignment? <ul><li>nome, endereco, telefone = “John Doe”, “Gotham City”, “+1 123 456 789” </li></ul><ul><li>puts nome </li></ul><ul><li>“ John Doe” </li></ul><ul><li>car, parts = “Peugeot”, [:engine, :wheels, :seats] </li></ul><ul><li>puts parts.size </li></ul><ul><li>2 </li></ul><ul><li>put parts.class </li></ul><ul><li>Array </li></ul>
  30. Yield that block, will ya’? <ul><li>def delegate_something(&block) </li></ul><ul><li>block.call(1, 2, 3) </li></ul><ul><li>end </li></ul><ul><li>delegate_something { |a, b, c| puts a, b, c } </li></ul><ul><li>1 2 3 </li></ul><ul><li>def delegate_with_yield(a, b, c) </li></ul><ul><li>puts “doing something…” </li></ul><ul><li>yield (a, b, c) </li></ul><ul><li>End </li></ul><ul><li>delegate_with_yield(1, 2, 3) do | a, b, c | </li></ul><ul><li>puts “#{a} #{b} #{c}” </li></ul><ul><li>end </li></ul><ul><li>“ doing something…” </li></ul><ul><li>“ 1 2 3”! </li></ul>
  31. JRuby! <ul><li>A ruby 1.8 implementation on the Java VM </li></ul><ul><ul><li>Currently on v0.92 </li></ul></ul><ul><ul><li>“ 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.) “ </li></ul></ul>
  32. Conclusion <ul><li>A very flexible and natural language </li></ul><ul><li>Based on few, but powerful concepts: closures, blocks, messages </li></ul><ul><li>Not a tyranical language: bad programmers can produce really nasty code with little or no restrictions... </li></ul>

×