How to iterate in Ruby?
I would like to iterate.
@some_value
outputs the following result:
{
"Meta" => {
"Query" => "java",
"ResultOffset" =开发者_如何学编程> "1",
"NumResults" => "1",
"TotalResults" => "21931"
}
}
I need to retrieve the value of each individual value. For example:
java
1
1
21931
@some_value["Meta"].values
output is array
["java", "1", "1", "21931"]
Hash#each_value
@some_value['Meta'].each_value { |v| p v }
There's the each method.
@some_value['Meta'].each do |k, v|
puts v
end
Which will loop through every of your entry and execute the code inside the do/end for every of them.
Here it'll display the value of the element.
精彩评论