[英]Iterating over array of Ruby Hashes and counting the no. of times value has appeared?
[英]Explanation of ruby code for iterating over an Array of hashes
我有两个脚本,它们的行为各不相同,一次运行两次,另两次运行两次,无法理解为什么任何一个plz都能帮助您?
脚本-1
@newArray=[{grade_id:"1",sections:"4"},
{grade_id:"2",sections:"8"},
{grade_id:"3",sections:"7"},
{grade_id:"4",sections:"7"},
{grade_id:"5",sections:"3"},
{grade_id:"6",sections:"3"},
{grade_id:"7",sections:"3"}]
@total_grades_passed = @newArray.count
@grades_counter = 0
while @grades_counter < @total_grades_passed do
@newArray[@grades_counter].each do |key, value|
print [key]
puts [value]
end
@grades_counter += 1
end
给出以下输出。
[:grade_id]1
[:sections]4
[:grade_id]2
[:sections]8
[:grade_id]3
[:sections]7
[:grade_id]4
[:sections]7
[:grade_id]5
[:sections]3
[:grade_id]6
[:sections]3
[:grade_id]7
[:sections]3
而SCRIPT-2则提供完全不同的输出。
@newArray=[{grade_id:"1",sections:"4"},
{grade_id:"2",sections:"8"},
{grade_id:"3",sections:"7"},
{grade_id:"4",sections:"7"},
{grade_id:"5",sections:"3"},
{grade_id:"6",sections:"3"},
{grade_id:"7",sections:"3"}]
@total_grades_passed = @newArray.count
@grades_counter = 0
while @grades_counter < @total_grades_passed do
@newArray[@grades_counter].each do |x,y|
@grade_id = @newArray[@grades_counter][:grade_id].to_i
@no_of_sections = @newArray[@grades_counter][:sections].to_i
puts "Grade id is #{@grade_id} and its class is #{@grade_id.class}"
puts "section is #{@no_of_sections} and its class is #{@no_of_sections.class}"
end
@grades_counter += 1
end
提供以下奇怪的输出。
Grade id is 1 and its class is Fixnum
section is 4 and its class is Fixnum
Grade id is 1 and its class is Fixnum
section is 4 and its class is Fixnum
Grade id is 2 and its class is Fixnum
section is 8 and its class is Fixnum
Grade id is 2 and its class is Fixnum
section is 8 and its class is Fixnum
Grade id is 3 and its class is Fixnum
section is 7 and its class is Fixnum
Grade id is 3 and its class is Fixnum
section is 7 and its class is Fixnum
Grade id is 4 and its class is Fixnum
section is 7 and its class is Fixnum
Grade id is 4 and its class is Fixnum
section is 7 and its class is Fixnum
Grade id is 5 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 5 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 6 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 6 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 7 and its class is Fixnum
section is 3 and its class is Fixnum
Grade id is 7 and its class is Fixnum
section is 3 and its class is Fixnum
谁能解释一下为什么会这样,两个脚本及其后续输出之间的区别是由于哪几行代码引起的? 我只想提取grade_id和section_id并进行一些计算,然后移至下一对成绩和科目ID,并对它们进行相同的计算。
有更好,更清洁,更“红褐色”的方式来执行此操作。
如果grade_id和sections是您想要的,则可以这样访问:
@newArray.each do |hash|
hash.each do |grade_id, sections|
# do something
end
end
还请注意,当您遍历哈希时,哈希将传递与密钥相同的次数。 这就是为什么您的文本输出两次的原因。
@newArray[@grades_counter].each
内部的块代码将运行2次,因为每个哈希都有2对键值。
以及较短的代码版本:
@newArray.each do |hash|
hash.each do |key, value|
print [key]
puts [value]
end
end
在第二个示例中,您不想遍历哈希。 您正在遍历哈希键(即使您不使用它)。
您应该这样做:
@newArray=[{grade_id:"1",sections:"4"},{grade_id:"2",sections:"8"},
{grade_id:"3",sections:"7"},{grade_id:"4",sections:"7"},
{grade_id:"5",sections:"3"},{grade_id:"6",sections:"3"},
{grade_id:"7",sections:"3"}]
@total_grades_passed = @newArray.count
@grades_counter = 0
while @grades_counter < @total_grades_passed do
@grade_id = @newArray[@grades_counter][:grade_id].to_i
@no_of_sections = @newArray[@grades_counter][:sections].to_i
puts "Grade id is #{@grade_id} and its class is #{@grade_id.class}"
puts "section is #{@no_of_sections} and its class is #{@no_of_sections.class}"
@grades_counter += 1
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.