ruby para-programadores-php
TRANSCRIPT
![Page 1: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/1.jpg)
Ruby
para programadores PHP
Monday, April 25, 2011
![Page 2: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/2.jpg)
PHP
• Criada por Rasmus Lerdorf em 1994.
• Objetivo: Fazer um contador para a página pessoal de Rasmus.
• Originalmente era apenas uma biblioteca Perl.
• PHP3 escrito por Andi Gutmans e Zeev Suraski (Zend) em 1997/98
História
Monday, April 25, 2011
![Page 3: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/3.jpg)
Ruby
• Criada por Yukihiro Matsumoto (Matz) em 1993.
• Objetivo: Criar uma linguagem poderosa que tenha uma “versão simplificada” de programação funcional com ótima OO.
• Matz: “I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided to design my own language”
• Matz: “I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language.”
História
Monday, April 25, 2011
![Page 4: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/4.jpg)
PHP
Variáveis
$number = 18;$string = “John”;$bool = true;$array = array(7,8,6,5);$hash = array(“foo” => “bar”);$obj = new Class(“param”);
Monday, April 25, 2011
![Page 5: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/5.jpg)
Ruby
Variáveis
number = 18string = “John”another_string = %(The man said “Wow”)bool = truearray = [7,8,6,5];word_array = %w{hello ruby world}hash = {:foo => ‘bar’} # new_hash = {foo:‘bar’}obj = Class.new(“param”)
# news!
ages = 18..45 # rangecep = /^\d{5}-\d{3}$/ # regular expression
Monday, April 25, 2011
![Page 6: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/6.jpg)
PHP
• Procedural com suporte a OO.
Paradigma
$a = array(1,2,3);array_shift($a);=> 1array_pop($a);=> 3array_push($a, 4);=> [2,4]
Monday, April 25, 2011
![Page 7: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/7.jpg)
Ruby
• Procedural, totalmente OO com classes (Smalltalk-like), um tanto funcional com o conceito de blocos. Not Haskell thought.
Paradigma
a = [1,2,3]a.shift=> 1a.pop=> 3a.push(4)=> [2,4]
Monday, April 25, 2011
![Page 8: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/8.jpg)
PHP
• Dinâmica e fraca.
Tipagem
10 + “10”;=> 20
Monday, April 25, 2011
![Page 9: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/9.jpg)
Ruby
• Dinâmica e forte. (Aberta a mudanças.)
Tipagem
10 + “10”=> TypeError: String can't be coerced into Fixnum
class Fixnum alias :old_sum :+ def + s old_sum s.to_i endend10 + “10”=> 20
Monday, April 25, 2011
![Page 10: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/10.jpg)
Ruby
• ...como???
Tipagem
1 + 1=> 2
1.+(1)=> 2
1.send(‘+’, 1)=> 2
# “Operações”? Métodos!
Monday, April 25, 2011
![Page 11: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/11.jpg)
PHP
• if, switch, ternário;
Fluxo
if($i == $j){ ... }
$i == $j ? ... : ...
switch($i){ case(“valor”){ TODO }}
Monday, April 25, 2011
![Page 12: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/12.jpg)
Ruby
• if, unless ...
Fluxo
if i == j ...end
unless cond ...end
puts “Maior” if age >= 18
puts “Menor” unless user.adult?
Monday, April 25, 2011
![Page 13: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/13.jpg)
Ruby
• ...case...
Fluxo
# default usagecase hour when 1: ... when 2: ...end
# with ranges!case hour when 0..7, 19..23: puts “Good nite” when 8..12: puts “Good mornin’”end
Monday, April 25, 2011
![Page 14: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/14.jpg)
Ruby
• ...case...
Fluxo
# with regexescase date when /\d{2}-\d{2}-\d{4}/: ... when /\d{2}\/\d{2}\/\d{4}/: ...end
# crie seu próprio caseclass MyClass def === ... endend
Monday, April 25, 2011
![Page 15: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/15.jpg)
PHP
• while, for, foreach;
Iteradores
while($i < 10){ ... }
for($i = 0; $i < length($clients); $i++){ ... }
foreach($clients as $client){ ... }
Monday, April 25, 2011
![Page 16: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/16.jpg)
Ruby
• for in, each, map, select, inject... enumeradores;
Iteradores
5.times{ ... }
[5,7,4].each{ ... }
[1,2,3].map{|i| i + 1 }=> [2,3,4]
[16,19,22].select{|i| i >= 18 }=> [19,22]
[5,7,8].inject{|s,i| s + i }=> 20
Monday, April 25, 2011
![Page 17: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/17.jpg)
Ruby
• crie seu próprio iterador:
Iteradores / Blocos
def hi_five yield 1; yield 2; yield 3; yield 4; yield 5end
hi_five{|i| ... }
Monday, April 25, 2011
![Page 18: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/18.jpg)
Ruby
• power to the people.
Blocos
File.open(“file”, “w”){|f| f.write(“Wow”) }
File.each_line(“file”){|l| ... }
Dir.glob(“*.png”){ ... }
“Ruby para programadores PHP”.gsub(/PHP/){|m| ... }
get “/” { ... } # sinatra
p{ span “Ruby is ”; strong “cool” } # markaby
Monday, April 25, 2011
![Page 19: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/19.jpg)
PHP
• Herança comum, classes abstratas, interfaces. Como Java.
OO
Monday, April 25, 2011
![Page 20: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/20.jpg)
Ruby
• Classes e módulos.
OO
module Walker def walk ... endend
# módulo como “herança múltipla” ou “mixin”class Johny include Walkerend
Monday, April 25, 2011
![Page 21: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/21.jpg)
Ruby
• Classes e módulos.
OO
# módulo como “namescope”module Foo class Bar ... endend
variable = Foo::Bar.new
Monday, April 25, 2011
![Page 22: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/22.jpg)
PHP
• __call: Chama métodos não existentes.
• __get: Chama “atributos” não existentes.
• __set: Chama ao tentar setar atributos não existentes;
OO Dinâmico
$obj->metodo();
$obj->atributo;
$obj->atributo = “valor”;
Monday, April 25, 2011
![Page 23: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/23.jpg)
Ruby
• method_missing: Tudo em Ruby são chamadas de métodos.
OO Dinâmico
obj.metodo # chama o método “metodo”
obj.atributo # chama o método “atributo”
obj.atributo = “valor” # chama o método “atributo=”
class Foo def method_missing m, *args ... endend
Monday, April 25, 2011
![Page 24: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/24.jpg)
Ruby
• inherited...
OO Dinâmico
# inheritedclass Foo def self.inherited(subklass) ... endend
class Bar < Fooend
Monday, April 25, 2011
![Page 25: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/25.jpg)
Ruby
• included...
OO Dinâmico
# includedmodule Foo def included(klass) ... endend
class Bar include Fooend
Monday, April 25, 2011
![Page 26: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/26.jpg)
Ruby
• class_eval, class_exec....
OO Dinâmico
class Foo; end
Foo.class_eval(“def bar() ... end”)Foo.class_exec{ def bar() ... end }
Foo.bar # worksFoo.baz # works
Monday, April 25, 2011
![Page 27: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/27.jpg)
Ruby
• instance_eval, instance_exec, define_method....
OO Dinâmico
class Foo define_method(:bar) { ... } instance_exec{ def baz(); ... end } instance_eval(“def qux(); ... end”)end
Foo.new.bar # worksFoo.new.baz # worksFoo.new.qux # works
Monday, April 25, 2011
![Page 28: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/28.jpg)
Ruby
• attr_(reader|accessor|writer)
OO Dinâmico
class Foo attr_accessor :barend
# same as...class Foo def bar() @bar end def bar= val @bar = val endend
Monday, April 25, 2011
![Page 29: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/29.jpg)
Ruby
• nesting, alias, autoload, class_variable_(set|get|defined?), const_(get|set|defined?|missing), constanst, instance_(method|methods), method_(added|defined?|removed|undefined), remove_(class_variable|const|method), undef_method, and so much more...
OO Dinâmico
Monday, April 25, 2011
![Page 30: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/30.jpg)
PHP
• PECL, PEAR. ... PHP Classes?
Comunidade
Monday, April 25, 2011
![Page 31: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/31.jpg)
Ruby
• RubyGems
Comunidade
gem install bundler # install gem
bundler gem my_gem # create my own gem
cd my_gem
rake release # that’s all folks
#alsobundler install # install all dependencies for a project
Monday, April 25, 2011
![Page 32: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/32.jpg)
Ruby
• GitHub
Comunidade
Monday, April 25, 2011
![Page 33: Ruby para-programadores-php](https://reader033.vdocuments.mx/reader033/viewer/2022052822/554f581eb4c905524c8b52f9/html5/thumbnails/33.jpg)
Monday, April 25, 2011