Ruby
Introdução
Ruby
Ruby
Ruby
Ruby
Ruby
Ruby
def soma(x,y)
x+y
end
Ruby
If 2>1 and 2>0
puts “oi”
elsif 2>3
puts “oi2”
else
puts “oi3”
end
Ruby
achou = true
unless achou
puts “não achou”
else
puts “achou”
end
Ruby
4.times do
puts “oi\n”
end
Ruby
while(a<5)
puts a
a++
end
i=0
f=5
for i in (i..f)
puts i
end
Ruby
Ruby
friends = ["Melissa", "Jeff", "Ashley", "Rob"]
friends.each do |friend|
puts "I have a friend called " + friend
end
Ruby
friend = {
"first name" => "Jeffrey",
"last name" => "Biggs",
"address" => "34 Airport Rd",
"city" => "Toronto",
:province" => "Ontario"
}
Ruby
Ruby
Classes
Objetos
Orientação a Objetos
Em Ruby...
#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
Em Ruby...
Em Ruby...
def street
@street
end
def street=(value)
@street = value
end
Em Ruby...
Em Ruby...
Class Person
attr_accessor :first_name, :address
def initialize
@first_name = “ ”
@address = Address.new
end
end
Em Ruby...
Ruby
Ruby
Testes Unitários
Ruby
???????
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
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