1 / 28

Ruby

Ruby. Introdução. Ruby. O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por { ... } ou do ... end Não requer declaração de variáveis É Livre!. Ruby. Operadores aritméticos: + - / * ** % Operadores relacionais:

Download Presentation

Ruby

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby Introdução

  2. Ruby • O que é Ruby? • Interpretada / Orientada a Objetos • Sintaxe simples / Case Sensitive • Herança Única • Blocos delimitados por { ... } ou do ... end • Não requer declaração de variáveis • É Livre!

  3. Ruby • Operadores aritméticos: • + - / * ** % • Operadores relacionais: • == != > < >= <= • Operadores lógicos: • and or

  4. Ruby • Tipos Númericos • Integer • 2.is_a?(Integer)‏ • Float • 2.5.is_a?(Float)‏

  5. Ruby • String • “Hello World” • “Hello World” + “ Hello Bahia” • “Hello World ” * 3 (Estranho não?)‏

  6. Ruby • Variáveis • Exemplo: nome = “João” • Dinamicamente tipada • nome = “João” (String)‏ • nome = 25.6 (Float)‏ • Constantes • Iniciam com primeira letra maiúscula e pode ser redefinida • Pi = 3,14

  7. Ruby • Funções def soma(x,y)‏ x+y end • Métodos Predicados • Terminam com ? e retornam true ou false • Utilizados para testar uma condição • Ex: [1,2,3].include? 1 => true

  8. Ruby • Condicional - IF • If ... elsif ... else ... End • Ex: If 2>1 and 2>0 puts “oi” elsif 2>3 puts “oi2” else puts “oi3” end

  9. Ruby • Condicional – Unless • Significa “Se não” • Negativa do if • Ex: achou = true unless achou puts “não achou” else puts “achou” end

  10. Ruby • Loops 4.times do puts “oi\n” end

  11. Ruby • Loops • While: while(a<5)‏ puts a a++ end • Loops • For i=0 f=5 for i in (i..f) puts i end

  12. Ruby • Arrays – Índice inicia em 0 • num = [1,2,3,4] • num[0] => 1 • num << 45 OU num.push 45 • num.sort • num.reverse • num.length

  13. Ruby • Arrays – Iterator friends = ["Melissa", "Jeff", "Ashley", "Rob"] friends.each do |friend| puts "I have a friend called " + friend end

  14. Ruby • Hashes friend = { "first name" => "Jeffrey", "last name" => "Biggs", "address" => "34 Airport Rd", "city" => "Toronto", :province" => "Ontario" } • puts friend[city] => “Toronto”

  15. Ruby • Exercício

  16. Ruby Classes Objetos

  17. Orientação a Objetos • Em Ruby .... • Classe: String • Objeto: “456” • Método: to_i • Exemplo: • “oi”.methods

  18. Em Ruby... • Declarando Classes #Classe Address.rb Class Address => Define uma classe def initialize(street) => Construtor com um parâmetro @street = street => @street variável de instância privada end => fim do método end => fim da classe

  19. Em Ruby... • Com instanciar a classe? • addr = Address.new(“Av. Adhemar de Barros”)‏ • Métodos “Especiais” • getters => Objetivo de recuperar o valor de um detrminado atributo do objeto • setter => Objetivo de setar o valor de um determinado atributo do objeto

  20. Em Ruby... • Ainda na classe Address def street @street end def street=(value) @street = value end

  21. Em Ruby... • Podemos fazer... • addr.address => Retorna “Av. Adhemar de Barros” • addr.address= “Campo Grande” • addr.address => Retorna “Campo Grande”

  22. Em Ruby... • Exemplo: Class Person attr_accessor :first_name, :address def initialize @first_name = “ ” @address = Address.new end end

  23. Em Ruby... • Herança • Operador: < • Sobrecarga de métodos • Não existe em Ruby! • Variável de Classe • Iniciam com @@ • Método de classe • Nome_classe.Nome_Metodo

  24. Ruby • Exercício

  25. Ruby Testes Unitários

  26. Ruby • Testes Unitários ???????

  27. Ruby - Classe a ser testada class Fatorial def fatorial(n) if(n == 0 or n==1) 1 else n*fatorial(n-1) end end end

  28. Ruby – Unidade de Testes require 'test/unit' require "Fatorial" class TC_Fatorial < Test::Unit::TestCase def setup @fatorial = Fatorial.new puts "setup" end def test_fatorial_0 assert_equal(1,@fatorial.fatorial(0), "fat(0) = 1") end def test_fatorial_1 assert_equal(1,@fatorial.fatorial(1), "fat(1) = 1") end def test_fatorial_6 assert_equal(1,@fatorial.fatorial(1), "fat(6) = 24") end def test_numero_negativo assert_raise(SystemStackError) do @fatorial.fatorial(-1) end end def teardown @fatorial = nil puts "teardown" end end

More Related