Ruby 从名称和变量数组创建哈希(不能将数组转换为字符串)
我想构建一个嵌套的哈希数组。我有一个数组,Ruby 从名称和变量数组创建哈希(不能将数组转换为字符串),ruby,hash,Ruby,Hash,我想构建一个嵌套的哈希数组。我有一个数组,分支,看起来像这样: branch = ["handbags", "womens-shoes", "womens-beauty", "womens-clothes"] branch = {"handbags" => ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"], "wom
分支
,看起来像这样:
branch = ["handbags", "womens-shoes", "womens-beauty", "womens-clothes"]
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
然后,我有另一个数组,其中包含与手提包配套的东西:
handbags = ["wallets", "backpacks", "clutches", "evening-handbags", "hobo-bags", "satchels", "shoulder-bags", "tote-bags"]
我想在一个循环中,将手提包
数组插入分支
数组,这样我可以得到如下结果:
branch = ["handbags", "womens-shoes", "womens-beauty", "womens-clothes"]
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
我试着这样做:
branch = ["handbags", "womens-shoes", "womens-beauty", "womens-clothes"]
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"]
给我
无法将数组转换为整数
我怎样才能做到这一点呢?你不能用一个不同于整数的对象为数组建立索引。你必须使用散列。
由于branch是一个数组,Ruby希望每个元素都有一个整数作为索引,当您给它另一个数组时,它会尝试将其转换为整数,从而产生错误。
试试这个:
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\"]}"
不能为对象与整数不同的数组编制索引。你必须使用散列。
由于branch是一个数组,Ruby希望每个元素都有一个整数作为索引,当您给它另一个数组时,它会尝试将其转换为整数,从而产生错误。
试试这个:
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\"]}"
代码中存在索引问题。声明:
branch[[branch_index], branch[branch_index]]
Ruby将其理解为试图在索引处访问分支(它不是一个有效的索引…它是一个数组)并查看分支(它是一个字符串)的许多元素(也不是一个有效的元素数…它是一个字符串)
我认为你真正想要的是:
def insert_branch(branch, branch_index, candidate, candidate_index)
branch[branch_index] = {branch[branch_index] => candidate[candidate_index]}
end
代码中存在索引问题。声明:
branch[[branch_index], branch[branch_index]]
Ruby将其理解为试图在索引处访问分支(它不是一个有效的索引…它是一个数组)并查看分支(它是一个字符串)的许多元素(也不是一个有效的元素数…它是一个字符串)
我认为你真正想要的是:
def insert_branch(branch, branch_index, candidate, candidate_index)
branch[branch_index] = {branch[branch_index] => candidate[candidate_index]}
end
红宝石说:
数组是任何对象的有序整数索引集合
所以问题是索引必须是整数,但您试图使用字符串索引分配给数组。您可能会考虑将整个内容转换为哈希(以便可以使用字符串作为键),或者可以将哈希或子数组放在数组中的适当索引中,例如:
idx = branch.index "handbags"
branch[idx] = { "handbags" => handbags }
你还应该看看Array#zip,看看它是否能提供你所需要的功能。Ruby说:
数组是任何对象的有序整数索引集合
所以问题是索引必须是整数,但您试图使用字符串索引分配给数组。您可能会考虑将整个内容转换为哈希(以便可以使用字符串作为键),或者可以将哈希或子数组放在数组中的适当索引中,例如:
idx = branch.index "handbags"
branch[idx] = { "handbags" => handbags }
您还应该看看Array#zip,看看它是否能为您提供所需的功能。您所拥有的功能有些不足。也许这会给你一些指导
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"]
你的东西有点不对劲。也许这会给你一些指导
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"]
您想做的事情可以通过以下方式完成:
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
获取类别命名为值的数组。您尝试执行的操作只需使用:
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 = ["foo", "bar"]
foo = ...
bar = ...
那么您就不走运了:您无法(以任何好的方式)根据局部变量的名称获取其引用的对象。当您发现自己想要按名称查找某个值时,应该使用散列来存储这些值,以便以后检索。i、 e.您应该将数据结构改为:
array = ["foo","bar"]
things = {
"foo" => ...,
"bar" => ...
}
…但是当然,你不需要做更多的工作就可以得到你的答案
如果出于某种原因,您无法将代码更改为使用单个哈希文本,但如果您可以将代码更改为使用实例变量而不是局部变量,则可以执行以下操作:
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"]}
如果你真的有:
array = ["foo", "bar"]
foo = ...
bar = ...
那么您就不走运了:您无法(以任何好的方式)根据局部变量的名称获取其引用的对象。当您发现自己想要按名称查找某个值时,应该使用散列来存储这些值,以便以后检索。i、 e.您应该将数据结构改为:
array = ["foo","bar"]
things = {
"foo" => ...,
"bar" => ...
}
…但是当然,你不需要做更多的工作就可以得到你的答案
如果出于某种原因,您无法将代码更改为使用单个哈希文本,但如果您可以将代码更改为使用实例变量而不是局部变量,则可以执行以下操作:
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"]}
你为什么给我们这些密码?您从不使用分支机构
,手提包
,或插入分支机构
。而且你从来没有定义过candidate
。我不认为[array]可以变成那样的{associative array}。但我其实不懂Ruby,所以你不应该听我的。你为什么要给我们这些代码?您从不使用分支机构
,手提包
,或插入分支机构
。而且你从来没有定义过candidate
。我不认为[array]可以变成那样的{associative array}。但是我并不真正了解Ruby,所以你不应该听我的。我认为OP试图根据数组中的字符串查找变量的值。比