28
Ruby Introdução

Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Embed Size (px)

Citation preview

Page 1: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Introdução

Page 2: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

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!

Page 3: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Operadores aritméticos:

+ - / * ** %

Operadores relacionais:

== != > < >= <=

Operadores lógicos:

and or

Page 4: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Tipos Númericos

Integer

2.is_a?(Integer)

Float

2.5.is_a?(Float)

Page 5: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

String

“Hello World”

“Hello World” + “ Hello Bahia”

“Hello World ” * 3 (Estranho não?)

Page 6: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

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

Page 7: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

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

Page 8: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

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

Page 9: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Condicional – Unless

Significa “Se não”

Negativa do if

Ex:

achou = true

unless achou

puts “não achou”

else

puts “achou”

end

Page 10: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Loops

4.times do

puts “oi\n”

end

Page 11: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Loops

While:

while(a<5)

puts a

a++

end

Loops

For

i=0

f=5

for i in (i..f)

puts i

end

Page 12: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

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

Page 13: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Arrays – Iterator

friends = ["Melissa", "Jeff", "Ashley", "Rob"]

friends.each do |friend|

puts "I have a friend called " + friend

end

Page 14: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Hashes

friend = {

"first name" => "Jeffrey",

"last name" => "Biggs",

"address" => "34 Airport Rd",

"city" => "Toronto",

:province" => "Ontario"

}

puts friend[city] => “Toronto”

Page 15: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Exercício

Page 16: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Classes

Objetos

Page 17: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Orientação a Objetos

Em Ruby ....

Classe: String

Objeto: “456”

Método: to_i

Exemplo:

“oi”.methods

Page 18: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

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

Page 19: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

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

Page 20: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Em Ruby...

Ainda na classe Address

def street

@street

end

def street=(value)

@street = value

end

Page 21: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Em Ruby...

Podemos fazer...

addr.address => Retorna “Av. Adhemar de Barros”

addr.address= “Campo Grande”

addr.address => Retorna “Campo Grande”

Page 22: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Em Ruby...

Exemplo:

Class Person

attr_accessor :first_name, :address

def initialize

@first_name = “ ”

@address = Address.new

end

end

Page 23: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

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

Page 24: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Exercício

Page 25: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Testes Unitários

Page 26: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby

Testes Unitários

???????

Page 27: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

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

Page 28: Ruby Introdução. Ruby O que é Ruby? Interpretada / Orientada a Objetos Sintaxe simples / Case Sensitive Herança Única Blocos delimitados por {... } ou

Ruby – Unidade de Testesrequire '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