2016-07-15 177 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")