I'm studying Ruby (without Rails, for now).
And I have 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
contato.rb
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 # > > (NoMethodError) from main.rb: 14: in ''
And I realized that if I add the flatten
method, it works as waiting.
puts agenda.contatos.flatten.map(&:email)
I just do not understand why, since flatten
does nothing special, it 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'm coming from java, the only 'functional' language I've ever worked with is javascript)