对于每行代码,都会给出一个描述。我已经完成了一半的程序并且停留在self
对象和转发程序中,所以请完成它以使其正常工作。
class Person
#have a first_name and last_name attribute with public accessors
#attr_accessor
attr_accessor :first_name , :last_name
#have a class attribute called `people` that holds an array of objects
@@people = []
#have an `initialize` method to initialize each instance
def initialize(first_name,last_name)#should take 2 parameters for first_name and last_name
@first_name = first_name
@last_name = last_name #assign those parameters to instance variables
#add the created instance (self) to people class variable
end
#have a `search` method to locate all people with a matching `last_name`
def self.search(last_name)
#accept a `last_name` parameter
#search the `people` class attribute for instances with the same `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
答案 0 :(得分:0)
要将已初始化的person
添加到类层次结构变量@@people
,请将initialize
方法更改为:
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
@@people << self
end