如何在 Ruby 中使用嵌套哈希的动态变量名称?

发布于 2024-12-01 16:25:16 字数 441 浏览 1 评论 0原文

我怎样才能在 Ruby (1.8) 中做类似的事情?我的目标是使用一个变量作为哈希中分配变量的键。

@keys=""
my_hash = Hash.new { |h,k| h[k]=Hash.new(&h.default_proc) }

line="long:keys:are:here:many:of:them:dont:know:how:much"

line.split(':').each { |x| 
  @keys=@keys+'["'+x+'"]'
}

my_hash#{@keys}=1

#I would like to assign a variable for the following.
# my_hash["long"]["keys"]["are"]["here"]["many"]["of"]["them"]["dont"]["know"]["how"]["many"]=1

How could I do something similar in Ruby (1.8)? My aim is to use a variable for the key in the hash where I assign a variable.

@keys=""
my_hash = Hash.new { |h,k| h[k]=Hash.new(&h.default_proc) }

line="long:keys:are:here:many:of:them:dont:know:how:much"

line.split(':').each { |x| 
  @keys=@keys+'["'+x+'"]'
}

my_hash#{@keys}=1

#I would like to assign a variable for the following.
# my_hash["long"]["keys"]["are"]["here"]["many"]["of"]["them"]["dont"]["know"]["how"]["many"]=1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2024-12-08 16:25:16

循环遍历要嵌套的项目,为每个项目创建一个新的哈希值并将其放入前一个哈希值中。由于您想为最后一个变量分配一个变量,因此您可以在构造变量时保留指向每个变量的指针,一旦拥有所有变量,您就拥有了指向最后一个变量的指针。代码如下所示:

hash = {}
line="long:keys:are:here:many:of:them:dont:know:how:much"

current_depth = hash
subhash_pointers = []
line.split(':').each { |x|
  current_depth[x] = {}
  current_depth = current_depth[x]
  subhash_pointers << current_depth
}

puts hash.inspect

subhash_pointers[-1] = 1
puts subhash_pointers.join(' ')

产生以下输出(即您正在查找的大哈希值和指向所有子哈希值的指针,最后一个根据您的要求为 1):

{"long"=>{"keys"=>{"are"=>{"here"=>{"many"=>{"of"=>{"them"=>{"dont"=>{"know"=>{"how"=>{"much"=>{}}}}}}}}}}}}

keysareheremanyofthemdontknowhowmuch areheremanyofthemdontknowhowmuch heremanyofthemdontknowhowmuch manyofthemdontknowhowmuch ofthemdontknowhowmuch themdontknowhowmuch dontknowhowmuch knowhowmuch howmuch much 1

Loop through the items to nest through, creating a new hash for each and putting it inside the previous hash. Since you want to assign a variable to the last one, you can keep pointers to each as you construct it and once you have them all, you have a pointer to the last one. The code looks like this:

hash = {}
line="long:keys:are:here:many:of:them:dont:know:how:much"

current_depth = hash
subhash_pointers = []
line.split(':').each { |x|
  current_depth[x] = {}
  current_depth = current_depth[x]
  subhash_pointers << current_depth
}

puts hash.inspect

subhash_pointers[-1] = 1
puts subhash_pointers.join(' ')

Which produces this output (namely the big hash you were looking for and pointers to all the subhashes, with the last one being 1 as you requested):

{"long"=>{"keys"=>{"are"=>{"here"=>{"many"=>{"of"=>{"them"=>{"dont"=>{"know"=>{"how"=>{"much"=>{}}}}}}}}}}}}

keysareheremanyofthemdontknowhowmuch areheremanyofthemdontknowhowmuch heremanyofthemdontknowhowmuch manyofthemdontknowhowmuch ofthemdontknowhowmuch themdontknowhowmuch dontknowhowmuch knowhowmuch howmuch much 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文