Your SlideShare is downloading. ×
0
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Ruby

709

Published on

O que Ruby está trazendo ao mundo de desenvolvimento de software.

O que Ruby está trazendo ao mundo de desenvolvimento de software.

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

  • Be the first to like this

No Downloads
Views
Total Views
709
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Ecossistema Ruby O que Ruby está trazendo ao mundo de desenvolvimento de software. @rafaeldx7
  • 2. @rafaeldx7 Rafael Almeida de Carvalho
  • 3. Ruby É uma linguagem de programação.
  • 4. Interpretada Multiparadigma Tipagem Dinâmica Tipagem Forte Código Aberto
  • 5. Criada porYukihiro "Matz" Matsumoto “I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python.” http://linuxdevcenter.com/pub/a/linux/2001/11/29/ruby.html
  • 6. Início do desenvolvimentoDez/1993 Dez/1994 Primeira versão alpha Dez/1995 Release 0.95 Dez/1996 Versão 1.0 Ago/2010 Versão 1.9.2
  • 7. Interpretadores
  • 8. MRI Matz Ruby Interpreter
  • 9. for .NET 4.0, Silverlight 4 and Windows Phone 7.
  • 10. blá blá blá... CADÊ O CÓDIGO!?
  • 11. Imprimir os números ímpares entre 1 e limit.
  • 12. def impares(limit) 1.upto(limit).select { |n| n.odd? } end
  • 13. Tipagem Dinâmica
  • 14. nome = "Rafael" # => "Rafael" nome.class # => String nome = :rafael # => :rafael nome.class # => Symbol
  • 15. Tipagem Forte!
  • 16. ruby $> 1 + "1"
  • 17. TypeError: String can't be coerced into Fixnum ! from (irb):21:in `+' ! from (irb):21 ruby $> 1 + "1"
  • 18. TUDO é um objeto!
  • 19. 3.times { puts "Hi!" } # Hi! # Hi! # Hi!
  • 20. "Ruby eh show!".split.reverse
  • 21. "Ruby eh show!".split.reverse => ["show!", "eh", "Ruby"]
  • 22. true.class => TrueClass true.methods.count => 48 true.to_s => “true”
  • 23. nil.class => NilClass 0.10.class => Float
  • 24. Métodos são objetos!
  • 25. 42.next # => 43 method = 42.method :next # => #<Method: Fixnum(Integer)#next> method.call # => 43 method.class # => Method
  • 26. Operadores são Métodos
  • 27. 2 + 2 # => 4 2.+(2) # => 4 2.public_methods.sort # => ["!", "!=", "!~", "%", "&", "*", "**", "+", "+@", "-", "-@", "/", "<", "<<","<=", "<=>", "==", "===", "=~", ">", ">=", ">>", "[]", "^", ... ]
  • 28. class Fixnum def +(value) "#{self} + #{value}" end def ==(value) self * value end end puts 4 + 4 # => "4 + 4" puts 3 == 2 # => 6
  • 29. Executar um método é enviar uma mensagem ao objeto.
  • 30. 2 + 2 #=> 4 2.send('+', 2) => 4 ['rafael', 'dx7'].join => "rafaeldx7" ['rafael', 'dx7'].send(:join) => "rafaeldx7"
  • 31. Métodos SEMPRE retornam valor!
  • 32. def um_metodo end um_metodo # nil def um_metodo 1 == 2 - 1 end um_metodo # true def um_metodo "Opa!" end um_metodo # "Opa!" def um_metodo (1..5).to_a end um_metodo #[1,2,3,4,5]
  • 33. def um_metodo return 1, 2, 3 end a, b, c = um_metodo puts a, b, c => #1 => #2 => #3
  • 34. Metaprogramação
  • 35. class Foo end Foo.class_eval do define_method("novo") do |arg| puts arg end end f = Foo.new f.novo("123") => # 123
  • 36. class Foo def method_missing(name) puts "método #{name} desconhecido" end end f = Foo.new f.xpto => método xpto desconhecido
  • 37. Mais JRuby
  • 38. fizzbuzz
  • 39. def fizzbuzz(number) resp = "" resp.tap do resp << "fizz" if number.modulo(3) == 0 resp << "buzz" if number.modulo(5) == 0 resp << number.to_s if resp.empty? end end
  • 40. def fizzbuzz(number) resp = "" resp.tap do resp << "fizz" if number.modulo(3) == 0 resp << "buzz" if number.modulo(5) == 0 resp << number.to_s if resp.empty? end end pane = javax.swing.JOptionPane numbers = pane.show_input_dialog("Quantos números?").to_i
  • 41. def fizzbuzz(number) resp = "" resp.tap do resp << "fizz" if number.modulo(3) == 0 resp << "buzz" if number.modulo(5) == 0 resp << number.to_s if resp.empty? end end pane = javax.swing.JOptionPane list = java.util.ArrayList.new numbers = pane.show_input_dialog("Quantos números?").to_i numbers.times do |i| list << fizzbuzz(i + 1) end
  • 42. def fizzbuzz(number) resp = "" resp.tap do resp << "fizz" if number.modulo(3) == 0 resp << "buzz" if number.modulo(5) == 0 resp << number.to_s if resp.empty? end end pane = javax.swing.JOptionPane list = java.util.ArrayList.new numbers = pane.show_input_dialog("Quantos números?").to_i numbers.times do |i| list << fizzbuzz(i + 1) end pane.show_message_dialog(nil, list.to_s)
  • 43. require 'java' def fizzbuzz(number) resp = "" resp.tap do resp << "fizz" if number.modulo(3) == 0 resp << "buzz" if number.modulo(5) == 0 resp << number.to_s if resp.empty? end end pane = javax.swing.JOptionPane list = java.util.ArrayList.new numbers = pane.show_input_dialog("Quantos números?").to_i numbers.times do |i| list << fizzbuzz(i + 1) end pane.show_message_dialog(nil, list.to_s)
  • 44. $ ruby fizzbuzz.rb
  • 45. $ ruby fizzbuzz.rb
  • 46. $ ruby fizzbuzz.rb
  • 47. DSL Domain Specific Language
  • 48. describe Account do context "transfering money" do it "deposits transfer amount to the other account" do source = Account.new(50, :USD) target = mock('target account') target.should_receive(:deposit).with(Money.new(5, :USD)) source.transfer(5, :USD).to(target) end it "reduces its balance by the transfer amount" do source = Account.new(50, :USD) target = stub('target account') source.transfer(5, :USD).to(target) source.balance.should == Money.new(45, :USD) end end end RSpec - Testando código Ruby
  • 49. # language: pt Funcionalidade: Usuário gerenciando produtos Como um usuário com permissão de gerenciar produtos Com o objetivo de dividir responsabilidades E custos por produto Eu deveria poder cadastrar E gerenciar uma lista de produtos Cenário: Listar produtos Dado que eu esteja logado E que existem os produtos produto 1, produto 2 E que eu esteja na página inicial Quando eu clico "Produtos" Então eu devo ver "produto 1" E eu devo ver "produto 2" Cucumber - Testes de Aceitação
  • 50. RubyGems
  • 51. $ gem install ruby-bitly Successfully installed mime-types-1.16 Successfully installed rest-client-1.6.0 Successfully installed json_pure-1.4.3 Successfully installed ruby-bitly-0.1.3 4 gems installed $ gem list *** LOCAL GEMS *** json_pure (1.4.3) mime-types (1.16) rest-client (1.6.0) ruby-bitly (0.1.3) Instalando uma gem
  • 52. $ bitly http://rafaeldx7.github.com Short url: http://bit.ly/aRr9aH Usando a gem em linha de comando
  • 53. require 'rubygems' require 'ruby-bitly' bitly = Bitly.shorten 'http://rafaeldx7.github.com', 'rafaeldx7', 'R_xxxxxxxxxxxxxxxxxxxxxxxxxxx' puts bitly.url => "http://bit.ly/aRr9aH" Usando a gem em um código Ruby
  • 54. Um framework Web para Ruby Extraído de uma aplicação real Criado em julho/2004 É um conjunto de gems Uma DSL para aplicações web
  • 55. Instalação do Rails $ gem install rails Successfully installed activesupport-3.0.1 Successfully installed builder-2.1.2 Successfully installed i18n-0.4.2 Successfully installed activemodel-3.0.1 Successfully installed rack-1.2.1 Successfully installed rack-test-0.5.6 Successfully installed rack-mount-0.6.13 Successfully installed tzinfo-0.3.23 Successfully installed abstract-1.0.0 Successfully installed erubis-2.6.6 Successfully installed actionpack-3.0.1 Successfully installed arel-1.0.1 Successfully installed activerecord-3.0.1 Successfully installed activeresource-3.0.1 Successfully installed polyglot-0.3.1 Successfully installed treetop-1.4.8 Successfully installed mail-2.2.9 Successfully installed actionmailer-3.0.1 Successfully installed thor-0.14.4 Successfully installed railties-3.0.1 Successfully installed rails-3.0.1 21 gems installed
  • 56. Criando uma aplicação Rails $ rails new sc2010 create create README create Rakefile create config.ru create .gitignore create Gemfile create app create app/controllers/application_controller.rb create app/helpers/application_helper.rb create app/mailers create app/models . . . $ bundle install
  • 57. $ rails server
  • 58. Vamos fazer alguma coisa... $ rails generate scaffold task name:string done:boolean invoke active_record create db/migrate/20101111152244_create_tasks.rb create app/models/task.rb invoke test_unit create test/unit/task_test.rb create test/fixtures/tasks.yml route resources :tasks invoke scaffold_controller create app/controllers/tasks_controller.rb invoke erb create app/views/tasks create app/views/tasks/index.html.erb create app/views/tasks/edit.html.erb create app/views/tasks/show.html.erb create app/views/tasks/new.html.erb create app/views/tasks/_form.html.erb invoke test_unit create test/functional/tasks_controller_test.rb invoke helper create app/helpers/tasks_helper.rb invoke test_unit create test/unit/helpers/tasks_helper_test.rb invoke stylesheets create public/stylesheets/scaffold.css
  • 59. Criando o DB e iniciando o servidor $ rake db:migrate (in /Users/rafael/Sites/playground/sc2010) == CreateTasks: migrating ============================================== -- create_table(:tasks) -> 0.0026s == CreateTasks: migrated (0.0034s) ===================================== $ rails server => Booting WEBrick => Rails 3.0.1 application starting in development on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server [2010-11-11 13:25:52] INFO WEBrick 1.3.1 [2010-11-11 13:25:52] INFO ruby 1.8.7 (2010-08-16) [x86_64-darwin10.4.0] [2010-11-11 13:25:52] INFO WEBrick::HTTPServer#start: pid=7367 port=3000
  • 60. Uma tarefa precisa ter um nome class Task < ActiveRecord::Base validates_presence_of :name end
  • 61. class Task < ActiveRecord::Base validates_presence_of :name belongs_to :user has_many :statuses end
  • 62. Quem usa Rails?
  • 63. Como aprender?
  • 64. pine.fm/LearnToProgram
  • 65. aprendaaprogramar.rubyonrails.pro.br
  • 66. Foi divertido? =D
  • 67. Perguntas?
  • 68. Obrigado! @rafaeldx7

×