Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Observer em Ruby
module Subject
def initialize
@observers=[]
end
def add_observer(&observer)
@observers << observer
end
def delete_observer(observer)
@observers.delete(observer)
end
def notify_observers
@observers.each do |observer|
observer.call(self)
end
end
end
class Funcionario
include Subject
attr_accessor :nome, :titulo, :salario
def initialize( nome, titulo, salario )
super()
@nome = nome
@titulo = titulo
@salario = salario
end
def salario=(novo_salario)
@salario = novo_salario
notify_observers
end
end
jose = Funcionario.new('jose', 'Crane Operator', 30000)
jose.add_observer do |funcionario|
puts ("Aumentando salário do #{funcionario.nome}!")
puts("Seu novo salário é #{funcionario.salario}!")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment