【问题标题】:ruby get array variable index value inside arrayruby 获取数组内的数组变量索引值
【发布时间】:2014-12-21 10:07:40
【问题描述】:

有没有办法从数组中获取索引值?

class Datadump
 def ddlist()
     @tobethrown = [
        sofa = ["red", "blue", "striped"],
        toys = ["figures", "robots", "comics"]
        ]
 end
 def dddig(chkdump)
     #check index value of array from string
     #I expect [1][2]
 end
end

dumpoop = Datadump.new
dumpoop.ddlist()
dumpoop.dddig("comics")

如果内容只是 ["sofa", "toys"] 而不是它们作为变量,我可以使用 tobethrown.index() 获取值。我是 ruby​​ 新手,如果这是一个菜鸟问题,请原谅我。

【问题讨论】:

    标签: ruby arrays class indexing


    【解决方案1】:
    @tobethrown.each.with_index do |a, i| 
      j = a.index(chkdump) 
      return [i, j] if j
    end
    

    【讨论】:

    • Lukav,如果它回答了您的问题,请单击复选标记符号将此答案标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多