【发布时间】:2016-06-09 03:06:01
【问题描述】:
有没有办法在类对象数组中找到 的值。那么一旦发现它被包含,你能找到它所在的数组中的什么位置吗?例如
class SomeClass
def initialize(value,otherData)
@value = value
@otherData = otherData
end
end
x = 0
otherData = "foo"
list = Array.new
while (x < 3)
list.push SomeClass.new(x,otherData)
x += 1
end
我想找到 list[x].value where value=2 并找出数组中的位置。
【问题讨论】:
-
您的代码无效。什么是
value方法?您的驼峰式局部变量给人的印象非常糟糕。 -
我认为你只需要遍历对象数组,查看对象中的值,然后从那里开始
-
代码不是无效的,它只是示例,不想争论 camelCase 与 underscore_case
标签: arrays ruby sorting object