简体   繁体   English

用于遍历哈希数组的ruby代码的说明

[英]Explanation of ruby code for iterating over an Array of hashes

I have two scripts both behave differently one runs one time while other twice, cant understand why can any one plz help?? 我有两个脚本,它们的行为各不相同,一次运行两次,另两次运行两次,无法理解为什么任何一个plz都能帮助您?

SCRIPT - 1 脚本-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

Gives below output. 给出以下输出。

[: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

While SCRIPT - 2 gives completely different output. 而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

Gives the below strange output. 提供以下奇怪的输出。

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

Can anyone please explain me why this is happening, whats the difference between both scripts and their subsequent outputs are due to which lines of code? 谁能解释一下为什么会这样,两个脚本及其后续输出之间的区别是由于哪几行代码引起的? I just want to extract the grade_id and section_id and do some computation then move on to the next pair of grade and section ids and do the same computation on them. 我只想提取grade_id和section_id并进行一些计算,然后移至下一对成绩和科目ID,并对它们进行相同的计算。

There are better, cleaner, more "ruby-like" ways to do this. 有更好,更清洁,更“红褐色”的方式来执行此操作。

if grade_id and sections is what you want, you could access it like this: 如果grade_id和sections是您想要的,则可以这样访问:

@newArray.each do |hash|
  hash.each do |grade_id, sections|
    # do something
  end
end

Also please note that when you iterate through a hash, it will pass as many times as there are keys. 还请注意,当您遍历哈希时,哈希将传递与密钥相同的次数。 That is why your text got outputted twice. 这就是为什么您的文本输出两次的原因。

Block code inside @newArray[@grades_counter].each will run 2 times because each hash you have 2 pairs of key-value. @newArray[@grades_counter].each内部的块代码将运行2次,因为每个哈希都有2对键值。

And the shorter version of your code: 以及较短的代码版本:

@newArray.each do |hash|
  hash.each do |key, value|
    print [key]
    puts [value]
  end
end

You don't want to iterate over the Hash in the second example. 在第二个示例中,您不想遍历哈希。 You're iterating over the hash keys (even though you don't use it). 您正在遍历哈希键(即使您不使用它)。

You should be doing something like this: 您应该这样做:

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM