有人可以解释为什么迭代一个嵌套的数据结构,这种方式确实工作? [英] Can someone explain why this way of iterating a nested data structure does work?

查看:109
本文介绍了有人可以解释为什么迭代一个嵌套的数据结构,这种方式确实工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造这个数组

["studies", "theory", "form", "animal", "basic", "processes"]

从以下嵌套的数据结构(存储为 sorted_hash

[["studies", {:freq=>11, :cap_freq=>0, :value=>11}],
 ["theory", {:freq=>9, :cap_freq=>1, :value=>11}],
 ["form", {:freq=>9, :cap_freq=>1, :value=>11}],
 ["animal", {:freq=>12, :cap_freq=>0, :value=>12}],
 ["basic", {:freq=>10, :cap_freq=>1, :value=>12}],
 ["processes", {:freq=>13, :cap_freq=>0, :value=>13}]]

我困惑这是一个哈希并写了下面code实现,我的任务:

I confused this to be a hash and wrote the following code to achieve my task:

sorted_hash.each do |key,value|
  array.push key
end

和我真的得到了我想要的东西。但经过一些思考和撬玩弄我不知道为什么。在每个方法的红宝石文件以阵列只显示一个数据项变量的例子,如

And I really got what I wanted. But after some thinking and playing around in Pry I wonder why. The each method Ruby Doc for Arrays only shows examples with one item variable, as in

每个{|条款|块}→元

但我用两个变量作为我会为哈希做。将红宝石尝试匹配的给定项目的变量,在这种情况下成功成为2级阵列具有2的长度是多少?
它是推荐做这样?
是否有更地道的方式来做到这一点?

but I use two variables as I would do for Hashes. Will Ruby try to match the given item variables, which in this case succeeds as the 2nd level array has a length of 2? Is it recommendable to do it like that? Are there more idiomatic ways to do it?

推荐答案

答案如下,从平行作业的Ruby实现的方式。

The answer follows from the way "parallel assignment" is implemented in Ruby.

正如你可能知道的:

a,b,c   = 1,2,3
a #=> 1 
b #=> 2 
c #=> 3 

a,b,c   = [1,2,3]
a #=> 1 
b #=> 2 
c #=> 3 

a,b     = [1,2,3]
a #=> 1 
b #=> 2 

a,*b    = [1,2,3]
a #=> 1 
b #=> [2, 3] 

*a,b    = [1,2,3]
a #=> [1, 2] 
b #=> 3

a,(b,c) = [1,[2,3]]
a #=> 1 
b #=> 2 
c #=> 3 

a,(b,(c,d)) = [1,[2,[3,4]]]
a #=> 1 
b #=> 2 
c #=> 3 
d #=> 4 

最后两个例子采用歧义,有人preFER叫分解的。

The last two examples employ "disambiguation", which some people prefer to call "decomposition".

现在让我们看看如何应用到值赋值给块变量。

Now let's see how that applies to the assignment of values to block variables.

假设:

arr = [["studies", {:freq=>11, :cap_freq=>0, :value=>11}],
       ["theory",  {:freq=>9,  :cap_freq=>1, :value=>11}]]

和我们执行:

arr.each { |a| p a }
["studies", {:freq=>11, :cap_freq=>0, :value=>11}]
["theory", {:freq=>9, :cap_freq=>1, :value=>11}]

让我们来看看这个更仔细。定义:

Let's look at this more carefully. Define:

enum = arr.each
  #=> #<Enumerator: [["studies",   {:freq=>11, :cap_freq=>0, :value=>11}],
  #                  ["theory",    {:freq=>9,  :cap_freq=>1, :value=>11}]]:each> 

的第一个元素被传递到块和分配给该块变量 v

v = enum.next
  #=> ["studies",  {:freq=>11, :cap_freq=>0, :value=>11}]     

我们可能是preFER使用并行任务有两个块变量(后 enum.rewind 重置枚举):

We may be prefer to use parallel assignment with two block variables (after enum.rewind to reset the enumerator):

a,h = enum.next
a #=> "studies" 
h #=> {:freq=>11, :cap_freq=>0, :value=>11} 

这允许我们编写(例如):

That allows us to write (for example):

arr.each { |a,h| p h }
{:freq=>11, :cap_freq=>0, :value=>11}
{:freq=>9, :cap_freq=>1, :value=>11}

在这里我们不使用块变量 A 。如果是这样的话,我们可能与局部变量 _ 替换或可能 _a

Here we do not use the block variable a. When that is the case, we might replace it with the local variable _ or possibly _a:

arr.each { |_,h| p h }
arr.each { |_a,h| p h }

此提请注意一个事实,即 A 未使用,并可能有助于避免错误。关于差错,假设我们想:

This draws attention to the fact that a is not used and may help to avoid errors. Regarding errors, suppose we want:

[[1,2],[3,4]].map { |a,b| puts 1+b }
  #=> [3,5]

但在不经意间写的:

but inadvertently write:

[[1,2],[3,4]].map { |a,b| puts a+b }
  #=> [3,7]

它执行得很好(但会产生不正确的结果)。与此相反,

which executes just fine (but produces an incorrect result). By contrast,

[[1,2],[3,4]].map { |_,b| puts a+b }
  #NameError: undefined local variable or method 'a'

告诉我们有问题。

tells us there's a problem.

下面是你可以在平行分配和歧义块做一个更详细的例子。鉴于:

Here's a more elaborate example of what you can do in blocks with parallel assignment and disambiguation. Given:

h = { :a=>[1,2], :b=>[3,4] }

假设我们希望获得:

suppose we wish to obtain:

    { :a=>3, :b=>7 }

的一种方法如下:

One way is the following:

h.each_with_object({}) { |(a,(b,c)),g| g[a] = b+c }
  => {:a=>3, :b=>7}

这篇关于有人可以解释为什么迭代一个嵌套的数据结构,这种方式确实工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆