Here is my situation. I have 2 Arrays
这是我的情况。我有2个阵列
@names = ["Tom", "Harry", "John"]
@emails = ["tom@gmail.com", "h@gmail.com", "j@gmail.com"]
I want to combine these two into some Array/Hash called @list
so I can then iterate in my view something like this:
我想将这两个组合成一些名为@list的Array / Hash,这样我就可以在我的视图中迭代这样的东西:
<% @list.each do |item| %>
<%= item.name %><br>
<%= item.email %><br>
<% end %>
I'm having trouble understanding how I can achieve this goal. Any thoughts?
我无法理解如何实现这一目标。有什么想法吗?
5 个解决方案
#1
47
@names = ["Tom", "Harry", "John"]
@emails = ["tom@gmail.com", "h@gmail.com", "j@gmail.com"]
@list = @names.zip( @emails )
#=> [["Tom", "tom@gmail.com"], ["Harry", "h@gmail.com"], ["John", "j@gmail.com"]]
@list.each do |name,email|
# When a block is passed an array you can automatically "destructure"
# the array parts into named variables. Yay for Ruby!
p "#{name} <#{email}>"
end
#=> "Tom <tom@gmail.com>"
#=> "Harry <h@gmail.com>"
#=> "John <j@gmail.com>"
@urls = ["yahoo.com", "ebay.com", "google.com"]
# Zipping multiple arrays together
@names.zip( @emails, @urls ).each do |name,email,url|
p "#{name} <#{email}> :: #{url}"
end
#=> "Tom <tom@gmail.com> :: yahoo.com"
#=> "Harry <h@gmail.com> :: ebay.com"
#=> "John <j@gmail.com> :: google.com"
#2
15
Just to be different:
只是为了与众不同:
[@names, @emails, @urls].transpose.each do |name, email, url|
# . . .
end
This is similar to what Array#zip does except that in this case there won't be any nil padding of short rows; if something is missing an exception will be raised.
这类似于Array#zip所做的,除了在这种情况下不会有任何短行的nil填充;如果缺少某些东西,将引发异常。
#3
7
Hash[*names.zip(emails).flatten]
This will give you a hash with name => email.
这将为您提供名称=>电子邮件的哈希。
#4
3
You can use zip
to zip together the two arrays and then map
to create Item
objects from the name-email-pairs. Assuming you have an Item
class whose initialize
methods accepts a hash, the code would look like this:
您可以使用zip将两个数组压缩在一起,然后映射以从名称 - 电子邮件对创建Item对象。假设你有一个其初始化方法接受哈希的Item类,代码如下所示:
@list = @names.zip(@emails).map do |name, email|
Item.new(:name => name, :email => email)
end
#5
3
Try This
尝试这个
Hash[@names.zip(@emails)]
You have two arrays @names = ["Tom", "Harry", "John"]
你有两个数组@names = [“Tom”,“Harry”,“John”]
@emails = ["tom@gmail.com", "h@gmail.com", "j@gmail.com"]
@emails = [“tom@gmail.com”,“h @ gmail.com”,“j @ gmail.com”]
@names.zip(@emails) it merge @emails to the @names associated with their index like below [["Tom", "tom@gmail.com"], ["Harry", "h@gmail.com"], ["John", "j@gmail.com"]]
@ names.zip(@emails)它将@emails合并到与他们的索引关联的@names,如下面[[“Tom”,“tom@gmail.com”],[“Harry”,“h @ gmail.com”] ,[“John”,“j @ gmail.com”]]
Now we can convert this array into hash using Hash[@names.zip(@emails)]
现在我们可以使用Hash [@ names.zip(@emails)]将此数组转换为哈希
#1
47
@names = ["Tom", "Harry", "John"]
@emails = ["tom@gmail.com", "h@gmail.com", "j@gmail.com"]
@list = @names.zip( @emails )
#=> [["Tom", "tom@gmail.com"], ["Harry", "h@gmail.com"], ["John", "j@gmail.com"]]
@list.each do |name,email|
# When a block is passed an array you can automatically "destructure"
# the array parts into named variables. Yay for Ruby!
p "#{name} <#{email}>"
end
#=> "Tom <tom@gmail.com>"
#=> "Harry <h@gmail.com>"
#=> "John <j@gmail.com>"
@urls = ["yahoo.com", "ebay.com", "google.com"]
# Zipping multiple arrays together
@names.zip( @emails, @urls ).each do |name,email,url|
p "#{name} <#{email}> :: #{url}"
end
#=> "Tom <tom@gmail.com> :: yahoo.com"
#=> "Harry <h@gmail.com> :: ebay.com"
#=> "John <j@gmail.com> :: google.com"
#2
15
Just to be different:
只是为了与众不同:
[@names, @emails, @urls].transpose.each do |name, email, url|
# . . .
end
This is similar to what Array#zip does except that in this case there won't be any nil padding of short rows; if something is missing an exception will be raised.
这类似于Array#zip所做的,除了在这种情况下不会有任何短行的nil填充;如果缺少某些东西,将引发异常。
#3
7
Hash[*names.zip(emails).flatten]
This will give you a hash with name => email.
这将为您提供名称=>电子邮件的哈希。
#4
3
You can use zip
to zip together the two arrays and then map
to create Item
objects from the name-email-pairs. Assuming you have an Item
class whose initialize
methods accepts a hash, the code would look like this:
您可以使用zip将两个数组压缩在一起,然后映射以从名称 - 电子邮件对创建Item对象。假设你有一个其初始化方法接受哈希的Item类,代码如下所示:
@list = @names.zip(@emails).map do |name, email|
Item.new(:name => name, :email => email)
end
#5
3
Try This
尝试这个
Hash[@names.zip(@emails)]
You have two arrays @names = ["Tom", "Harry", "John"]
你有两个数组@names = [“Tom”,“Harry”,“John”]
@emails = ["tom@gmail.com", "h@gmail.com", "j@gmail.com"]
@emails = [“tom@gmail.com”,“h @ gmail.com”,“j @ gmail.com”]
@names.zip(@emails) it merge @emails to the @names associated with their index like below [["Tom", "tom@gmail.com"], ["Harry", "h@gmail.com"], ["John", "j@gmail.com"]]
@ names.zip(@emails)它将@emails合并到与他们的索引关联的@names,如下面[[“Tom”,“tom@gmail.com”],[“Harry”,“h @ gmail.com”] ,[“John”,“j @ gmail.com”]]
Now we can convert this array into hash using Hash[@names.zip(@emails)]
现在我们可以使用Hash [@ names.zip(@emails)]将此数组转换为哈希