I made a User
class containing a class method find_by_email
:
class User
attr_accessor :email
def initialize
@email="example@example.com"
end
def self.find_by_email(pattern)
ObjectSpace.each_object(self) do |object|
puts object.email+" "+(object.email.include? pattern).to_s
end
end
end
In irb
I try:
irb> user1=User.new
irb> user2=User.new
irb> user1.email="sergio@example.com"
irb> User.find_by_email "s"
which returns:
example@example.com false
sergio@example.com true
I would like find_by_email
to return an array with the matching emails. So for this example it should only return ["sergio@example.com"]
. How can I refactor the find_by_email
class to achieve this?