0

Given the following code:

class Person
  attr_accessor :first_name, :last_name    
  @@people = []

  def initialize(first_name,last_name)
    @first_name = first_name
    @last_name = last_name
    @@people.push(self)
  end

  def self.search(last_name)
    @last_name = last_name #accept a `last_name` parameter
    @@people.select { |person| person.last_name }
    #return a collection of matching instances
  end

  #have a `to_s` method to return a formatted string of the person's name
  def to_s
    #return a formatted string as `first_name(space)last_name`
  end
end

p1 = Person.new("John", "Smith")
p2 = Person.new("John", "Doe")
p3 = Person.new("Jane", "Smith")
p4 = Person.new("Cool", "Dude")

puts Person.search("Smith")

# Should print out
# => John Smith
# => Jane Smith

What do I need to do to return the output under the Should print out bit? I can get it to return the object id:

#<Person:0x007fa40c04cd08>
#<Person:0x007fa40c04c920>
#<Person:0x007fa40c04c5d8>
#<Person:0x007fa40c04c5b0>

One problem that I see with that without even knowing what is in each one: there should only be two values returned. So clearly the search portion is also wrong.

What should I be doing with this?

2
  • You should implement Person#to_s to do what the comment says - "# return a formatted string as first_name(space)last_name" - then iterate over what Person#search returns and call Person#to_s on each object. Commented Jul 15, 2016 at 4:39
  • Also your search method is incorrect. Commented Jul 15, 2016 at 17:25

2 Answers 2

1
    class Person
      attr_accessor :first_name, :last_name
      @@people = []

      def initialize(first_name,last_name)
       @first_name = first_name
       @last_name = last_name
       @@people.push(self)
      end

      def self.search(last_name)
       @@people.select { |person| person.last_name == last_name }
       #return a collection of matching instances
      end

      #have a `to_s` method to return a formatted string of the persons name
      def to_s
        "#{self.first_name} #{self.last_name}"
      end
    end

    p1 = Person.new("John", "Smith")
    p2 = Person.new("John", "Doe")
    p3 = Person.new("Jane", "Smith")
    p4 = Person.new("Cool", "Dude")

    puts Person.search("Smith").collect(&:to_s)

I have changed the self.search method to select the objects that have the same last name and obviously the to_s method + what gets puts out. Have a read test and let me know if you have any other questions

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I see that, aside from not doing anything with the to_s method just yet (I was waiting to figure out how to pare down to only the two expected return values first), I wasn't properly comparing person.last_name to @last_name in self.search. With that properly in place I am now only getting two objects returned. And now, the to_s method is doing what it should with the inserted code. Thanks, again!
0

Add condition and maping array on @@Person. Code is described here:

class Person
  attr_accessor :first_name, :last_name    
  @@people = []

  def initialize(first_name,last_name)
    @first_name = first_name
    @last_name = last_name
    @@people.push(self)
  end

  def self.search(last_name)
    @last_name = last_name #accept a `last_name` parameter
     @people = @@people.select { |person|  person.last_name == @last_name }.map{|p| p.first_name.to_s + ' ' + p.last_name.to_s}

    #return a collection of matching instances
  end

  #have a `to_s` method to return a formatted string of the person's name
  def to_s
    #return a formatted string as `first_name(space)last_name`
  end
end

p1 = Person.new("John", "Smith")
p2 = Person.new("John", "Doe")
p3 = Person.new("Jane", "Smith")
p4 = Person.new("Cool", "Dude")

puts Person.search("Smith")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.