2016-07-15 173 views
0

阵列返回对象考虑下面的代码:从红宝石

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 

什么我需要做Should print out位下返回输出?我能得到它返回对象ID:

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

的一个问题,我与看到,甚至不知道什么是每个人:应该只有返回两个值。显然,搜索部分也是错误的。

我该怎么做?

+0

你应该实现'Person#to_s'来完成评论所说的内容 - *“#返回格式化的字符串作为first_name(空格)last_name”* - 然后迭代在'Person#search'返回并在每个对象上调用'Person#to_s'。 –

+0

此外您的搜索方法不正确。 – andrewkday

回答

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) 

我已经改变了self.search方法来选择具有相同的姓氏,明显的to_s方法+是什么鬼推出的对象。阅读测试并让我知道你是否有其他问题

+0

谢谢。我看到,除了没有对'to_s'方法做任何事情(我正在等待弄清楚如何仅仅减少两个预期的返回值)之外,我没有正确比较'person.last_name'到'self.search'中的'@ last_name'。正确的地方,我现在只能得到两个返回的对象。现在,'to_s'方法正在执行插入的代码。再次感谢! – theillien

0

在@@ Person上添加条件和映射数组。 代码是在这里描述:

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")