This is my code
这是我的代码
lunch_order = {
"Ryan" => "wonton soup",
"Eric" => "hamburger",
"Jimmy" => "sandwich",
"Sasha" => "salad",
"Cole" => "taco"
}
lunch_order.each { |element| puts element }
I want the value to be printed out, but here, both the value and the key are printed.
我想把值打印出来,但是这里,值和键都被打印出来了。
6 个解决方案
#1
3
You can loop over the values only with each_value
只能使用each_value对值进行循环
h = { "a" => 100, "b" => 200 } h.each_value {|value| puts value }
You can read more about api of hash here.
您可以在这里阅读更多关于散列api的内容。
#3
1
In Ruby a hash contains Keys and Values. So in your case the keys are:
在Ruby中,散列包含键和值。在你的例子中,关键是:
Keys
键
- Ryan
- 瑞安
- Eric
- 埃里克
- Jimmy
- 吉米
- Sasha
- 萨沙
- Cole
- 科尔
And the Values for those keys are:
这些键的值是:
Values
值
- Wonton Soup
- 馄饨汤
- Hamburger
- 汉堡
- Sandwich
- 三明治
- Salad
- 沙拉
- Taco
- 墨西哥煎玉米卷
And all you'd need to do to call the values is use the each loops like you've done but instead of using element as the local variable within the block, you use something like this:
你需要做的就是像你做过的那样使用每个循环来调用这些值但是你不需要使用元素作为块中的局部变量,而是使用如下方法:
lunch_order.each do { |key, value| puts value }
#4
0
lunch_order.each_pair { |key,value| puts value }
lunch_order。each_pair{|键,值|赋值}
Docs: http://apidock.com/ruby/Hash/each_pair
文档:http://apidock.com/ruby/Hash/each_pair
#5
0
So many ways to do it in Ruby:
在Ruby中有很多方法可以做到:
lunch_order.values.each { |element| puts element }
#6
0
Given your original code
给你的原始代码
lunch_order = {
"Ryan" => "wonton soup",
"Eric" => "hamburger",
"Jimmy" => "sandwich",
"Sasha" => "salad",
"Cole" => "taco"
}
You created a hash, which consists of both keys and values. Keys are on the left, values on the right. To get only the values use
您创建了一个散列,它包含键和值。键在左边,值在右边。只获取使用的值
lunch_order.values.each{|value| puts value}
Hope this helps.
希望这个有帮助。
#1
3
You can loop over the values only with each_value
只能使用each_value对值进行循环
h = { "a" => 100, "b" => 200 } h.each_value {|value| puts value }
You can read more about api of hash here.
您可以在这里阅读更多关于散列api的内容。
#2
#3
1
In Ruby a hash contains Keys and Values. So in your case the keys are:
在Ruby中,散列包含键和值。在你的例子中,关键是:
Keys
键
- Ryan
- 瑞安
- Eric
- 埃里克
- Jimmy
- 吉米
- Sasha
- 萨沙
- Cole
- 科尔
And the Values for those keys are:
这些键的值是:
Values
值
- Wonton Soup
- 馄饨汤
- Hamburger
- 汉堡
- Sandwich
- 三明治
- Salad
- 沙拉
- Taco
- 墨西哥煎玉米卷
And all you'd need to do to call the values is use the each loops like you've done but instead of using element as the local variable within the block, you use something like this:
你需要做的就是像你做过的那样使用每个循环来调用这些值但是你不需要使用元素作为块中的局部变量,而是使用如下方法:
lunch_order.each do { |key, value| puts value }
#4
0
lunch_order.each_pair { |key,value| puts value }
lunch_order。each_pair{|键,值|赋值}
Docs: http://apidock.com/ruby/Hash/each_pair
文档:http://apidock.com/ruby/Hash/each_pair
#5
0
So many ways to do it in Ruby:
在Ruby中有很多方法可以做到:
lunch_order.values.each { |element| puts element }
#6
0
Given your original code
给你的原始代码
lunch_order = {
"Ryan" => "wonton soup",
"Eric" => "hamburger",
"Jimmy" => "sandwich",
"Sasha" => "salad",
"Cole" => "taco"
}
You created a hash, which consists of both keys and values. Keys are on the left, values on the right. To get only the values use
您创建了一个散列,它包含键和值。键在左边,值在右边。只获取使用的值
lunch_order.values.each{|value| puts value}
Hope this helps.
希望这个有帮助。