4
I’m studying Ruby (no Rails, for now).
And I’m on the following scenario:
agenda.Rb
require 'set'
class Agenda
def initialize
@contatos = Set.new
end
def contatos
@contatos.to_a
end
def query_by_email(email)
contatos.flatten.select { |e| e.email == email }
end
def add(*contato)
@contatos << contato
end
end
Rb contact.
class Contato
attr_accessor :nome
attr_accessor :numero
attr_accessor :email
def initialize(nome, numero, email)
@nome = nome
@numero = numero
@email = email
end
def hash
email.hash
end
def eql?(other)
other.class == self.class && other.email == @email
end
def to_s
"Nome: #{@nome}, Numero: #{@numero}, Email: #{@email}"
end
end
and my main.Rb class (which I use for testing..)
main.Rb
require File.expand_path("contato")
require File.expand_path("agenda")
agenda = Agenda.new
a = Contato.new('Rafael', '98-88891948', '[email protected]')
b = Contato.new('Rafael', '98-88891948', '[email protected]')
c = Contato.new('Renan', '98-88891948', '[email protected]')
d = Contato.new('Renan', '98-88891948', '[email protected]')
e = Contato.new('Renan', '98-88891948', '[email protected]')
agenda.add a,b,c,d,e
puts agenda.contatos.map(&:email)
the last line is returning me the following error:
main.Rb:14:in
map': undefined method
email' for # >> (Nameserver) from main.Rb:14:in `'
And I realized that if I add the method flatten
, works as expected.
puts agenda.contatos.flatten.map(&:email)
I just don’t understand why, since the flatten
does nothing special, just returns an array that already existed without making any kind of modification or if you want to change the type. Could someone explain to me why it worked?
(I am coming from java, the only 'functional' language I already work with is javascript)
Rafael by the reply of @Rohbarboza I thought of something else instead of using
@contatos = Set.new
you tried to use@contatos = []
?– Guilherme Nascimento