【问题标题】:Returning objects from array in ruby从ruby中的数组返回对象
【发布时间】:2016-11-18 04:17:52
【问题描述】:

给定以下代码:

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>

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

我该怎么办?

【问题讨论】:

  • 你应该实现 Person#to_s 来做评论所说的 - "# return a formatted string as first_name(space)last_name" - 然后迭代 Person#search 返回的内容并在每个对象上调用Person#to_s
  • 另外你的搜索方法不正确。

标签: arrays ruby object


【解决方案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 方法 + 输出的内容。进行阅读测试,如果您有任何其他问题,请告诉我

【讨论】:

  • 谢谢。我看到了,除了尚未对 to_s 方法做任何事情(我一直在等着弄清楚如何首先减少到两个预期的返回值),我没有正确地将 person.last_name 与 @987654324 进行比较@在self.search。有了正确的位置,我现在只返回了两个对象。现在,to_s 方法正在对插入的代码执行应有的操作。再次感谢!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2021-10-07
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2018-08-12
    相关资源
    最近更新 更多