Ruby散列到值数组。

时间:2021-01-11 21:17:05

I have this:

我有这个:

hash  = { "a"=>["a", "b", "c"], "b"=>["b", "c"] } 

and I want to get to this: [["a","b","c"],["b","c"]]

我想:[[“a”、“b”、“c”],[“b”、“c”]]

This seems like it should work but it doesn't:

这似乎应该行得通,但行不通:

hash.each{|key,value| value}
=> {"a"=>["a", "b", "c"], "b"=>["b", "c"]} 

Any suggestions?

有什么建议吗?

6 个解决方案

#1


214  

Also, a bit simpler....

同时,有点简单....

>> hash = { "a"=>["a", "b", "c"], "b"=>["b", "c"] }
=> {"a"=>["a", "b", "c"], "b"=>["b", "c"]}
>> hash.values
=> [["a", "b", "c"], ["b", "c"]]

Ruby doc here

Ruby文档在这里

#2


34  

I would use:

我将使用:

hash.map { |key, value| value }

#3


20  

hash.collect { |k, v| v }
#returns [["a", "b", "c"], ["b", "c"]] 

Enumerable#collect takes a block, and returns an array of the results of running the block once on every element of the enumerable. So this code just ignores the keys and returns an array of all the values.

Enumerable#collect获取一个块,并返回一个数组,该数组表示在可枚举元素的每个元素上运行该块的结果。所以这段代码忽略了键并返回所有值的数组。

The Enumerable module is pretty awesome. Knowing it well can save you lots of time and lots of code.

可枚举模块非常棒。熟悉它可以节省大量的时间和代码。

#4


6  

hash  = { :a => ["a", "b", "c"], :b => ["b", "c"] }
hash.values #=> [["a","b","c"],["b","c"]]

#5


2  

It is as simple as

这很简单

hash.values
#=> [["a", "b", "c"], ["b", "c"]]

this will return a new array populated with the values from hash

这将返回一个由hash值填充的新数组

if you want to store that new array do

如果你想存储新的数组

array_of_values = hash.values
#=> [["a", "b", "c"], ["b", "c"]]

array_of_values
 #=> [["a", "b", "c"], ["b", "c"]]

#6


2  

There is also this one:

还有一个

hash = { foo: "bar", baz: "qux" }
hash.map(&:last) #=> ["bar", "qux"]

Why it works:

为什么工作原理:

The & calls to_proc on the object, and passes it as a block to the method.

&调用对象上的to_proc,并将其作为块传递给方法。

something {|i| i.foo }
something(&:foo)

#1


214  

Also, a bit simpler....

同时,有点简单....

>> hash = { "a"=>["a", "b", "c"], "b"=>["b", "c"] }
=> {"a"=>["a", "b", "c"], "b"=>["b", "c"]}
>> hash.values
=> [["a", "b", "c"], ["b", "c"]]

Ruby doc here

Ruby文档在这里

#2


34  

I would use:

我将使用:

hash.map { |key, value| value }

#3


20  

hash.collect { |k, v| v }
#returns [["a", "b", "c"], ["b", "c"]] 

Enumerable#collect takes a block, and returns an array of the results of running the block once on every element of the enumerable. So this code just ignores the keys and returns an array of all the values.

Enumerable#collect获取一个块,并返回一个数组,该数组表示在可枚举元素的每个元素上运行该块的结果。所以这段代码忽略了键并返回所有值的数组。

The Enumerable module is pretty awesome. Knowing it well can save you lots of time and lots of code.

可枚举模块非常棒。熟悉它可以节省大量的时间和代码。

#4


6  

hash  = { :a => ["a", "b", "c"], :b => ["b", "c"] }
hash.values #=> [["a","b","c"],["b","c"]]

#5


2  

It is as simple as

这很简单

hash.values
#=> [["a", "b", "c"], ["b", "c"]]

this will return a new array populated with the values from hash

这将返回一个由hash值填充的新数组

if you want to store that new array do

如果你想存储新的数组

array_of_values = hash.values
#=> [["a", "b", "c"], ["b", "c"]]

array_of_values
 #=> [["a", "b", "c"], ["b", "c"]]

#6


2  

There is also this one:

还有一个

hash = { foo: "bar", baz: "qux" }
hash.map(&:last) #=> ["bar", "qux"]

Why it works:

为什么工作原理:

The & calls to_proc on the object, and passes it as a block to the method.

&调用对象上的to_proc,并将其作为块传递给方法。

something {|i| i.foo }
something(&:foo)