从名称和变量数组创建哈希(不能将Array转换为String)

时间:2012-05-03 16:22:05

标签: ruby hash

我想构建一个嵌套的哈希数组。我有一个数组branch,看起来像这样:

branch = ["handbags", "womens-shoes", "womens-beauty", "womens-clothes"]

然后我有另一个包含handbags的内容的数组:

handbags = ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]

我想在循环中将handbags数组插入到branch数组中,以便我得到这样的结果:

branch = {"handbags" => ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"], "womens-shoes"],...}

我这样尝试:

def insert_branch(branch, branch_index, candidate, candidate_index)
    branch[[branch_index], branch[branch_index]] ||= {}
    branch[[branch_index], branch[branch_index]] = candidate[candidate_index]  
end

其中

candidate[candidate_index] = ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]

给了我

  

无法将Array转换为Integer

我该如何做到这一点?

6 个答案:

答案 0 :(得分:4)

您不能使用不同于整数的对象来索引数组。你必须使用哈希。 由于branch是一个数组,Ruby期望一个Integer为每个元素编制索引,当你给它另一个数组时,它会尝试将它转换为一个Integer,从而产生错误。 试试这个:

branch = Hash.new #unnecessary, but makes the class explicit
handbags = ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]
branch["handbags"] = handbags

branch.inspect将产生:

"{\"handbags\"=>[\"wallets\", \"backpacks\", \"clutches\", \"evening-handbags\", \"hobo-bags\", \"satchels\", \"shoulder-bags\", \"tote-bags\"]}"

答案 1 :(得分:1)

如果你真的有:

array = ["foo", "bar"]
foo   = ...
bar   = ...

然后你运气不好:你不能(以任何好的方式)根据其名称获取局部变量引用的对象。每当您发现自己想要按名称查找值时,您应该使用Hash存储值以便稍后检索。即您应该将数据结构更改为:

array  = ["foo","bar"]
things = {
  "foo" => ...,
  "bar" => ...
}

...但当然,你 你的答案,而无需再做任何工作。

如果由于某种原因您无法更改代码以使用单个Hash文字,但是如果您可以更改代码而不是使用实例变量而不是局部变量,则可以执行以下操作:

array  = ["foo", "bar"]
@foo   = ...
@bar   = ...
branch = Hash[ array.map{ |name| [name,instance_variable_get("@#{name}")] } ]

上面的代码需要Ruby 1.9;如果您使用的是旧版1.8,请说出来。

见过:

irb(main):001:0> a = %w[foo bar jim]
#=> ["foo", "bar", "jim"]

irb(main):002:0> @foo = %w[ 1 2 3 ]
#=> ["1", "2", "3"]
irb(main):003:0> @bar = %w[ 4 5 6 ]
#=> ["4", "5", "6"]
irb(main):004:0> @jim = %w[ 7 8 9 ]
#=> ["7", "8", "9"]

irb(main):006:0> Hash[ a.map{ |name| [name,instance_variable_get("@#{name}")] } ]
#=> {"foo"=>["1", "2", "3"], "bar"=>["4", "5", "6"], "jim"=>["7", "8", "9"]}

答案 2 :(得分:0)

您的代码中存在索引问题。声明:

branch[[branch_index], branch[branch_index]]
Ruby正在理解

试图在索引branch访问[branch_index](这不是一个有效的索引......它是一个数组),并查看branch[branch_index]许多元素(这也不是有效数量的元素......它是一个字符串)。

我认为你真正想要的是:

def insert_branch(branch, branch_index, candidate, candidate_index)
    branch[branch_index] = {branch[branch_index] => candidate[candidate_index]}
end

答案 3 :(得分:0)

Ruby Array class说:

  

数组是有序的,任何对象的整数索引集合。

所以问题是您的索引必须是整数,但是您尝试使用字符串索引分配给数组。您可以考虑将整个事物转换为哈希值(以便您可以使用字符串作为键),或者您可以将哈希或子数组放在正确的索引中,如下所示:

idx = branch.index "handbags"
branch[idx] = { "handbags" => handbags }

您还应该看看Array#zip,看看它是否能为您提供所需的功能。

答案 4 :(得分:0)

你所拥有的是一点点。也许这会给你一些指导。

branch = ["handbags", "womens-shoes", "womens-beauty", "womens-clothes"]

handbags = ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]

def insert_branch(catagories,catagories_index,branch)
  catagories[catagories_index] = Hash[catagories[catagories_index],branch] 
end

insert_branch(branch,0,handbags)


>> p branch  # OUTPUT IS
>> [{"handbags"=>["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]}, "womens-shoes", "womens-beauty", "womens-clothes"]

答案 5 :(得分:0)

您尝试做的事情可以通过以下方式完成:

a = %w|Things Animals Cities|
things = %w|Desk Table Lamp|

a[0] = {a[0] => things}

a
=> [{"Things"=>["Desk", "Table", "Lamp"]}, "Animals", "Cities"]

在你的情况下,它是

branch[0] = {branch[0] => handbags}

编辑:使用元编程,您可以根据名称查找数组:

categories = %w|Things Animals Cities|
things = %w|Desk Table Lamp|
animals = %w|Dog Cat Turtle|
cities = %w|London Rome Madrid|

result = categories.map do |category| 
  { category => eval(category.downcase) }
end

result
=> [{"Things"=>["Desk", "Table", "Lamp"]},
    {"Animals"=>["Dog", "Cat", "Turtle"]},
    {"Cities"=>["London", "Rome", "Madrid"]}]

请注意使用eval将类别命名的Array作为值。