引用数组值中的对象属性

问题描述 投票:0回答:1

我正在努力解决逻辑难题,只想将线索与一个对象的多个实例进行比较,检查属性值以查看它们是否未知(9),true(1)或false(0),然后进行更新如果需要。

class Fruit
  attr_number :number
  attr_accessor :color, :variety
  def initialize(number)
    @number = number
    @color = { "red" => 1, "brown" => 9, "green" => 9 }
    @variety = { "grape" => 9, "apple" => 9, "kiwi" => 0 }
  end
end

@one = Fruit.new(1)
@two = Fruit.new(2)
@produce = [ @one, @two ]
@attribs = [ :color, :variety ]
@clue = [ ["red", "apple"], ["green", "grape"] ]

在绑定撬动中,@ one.color正确返回了我可以很容易地检查出线索的哈希,

但是在@produce和@ attribs-上嵌套了一组循环

@produce.each do |food|
  @attribs.each do |describe|
    print food.describe
  end
end

我收到一个错误...我希望了解的事情是如何组装对哈希的有效引用。我知道我可以存储@array = [@ one.color],并且可以,但是它不能使我的逻辑引擎正常工作。

arrays ruby hash attributes
1个回答
1
投票

您可能想要#public_send在这里:

#public_send
© www.soinside.com 2019 - 2024. All rights reserved.