Hi,
Is there a way to access the count / iteration# within an “each” loop?
i.e.
how many times through the loop one is?
testhash.each { |d|
puts
}
Tks
Greg
Hi,
Is there a way to access the count / iteration# within an “each” loop?
i.e.
how many times through the loop one is?
testhash.each { |d|
puts
}
Tks
Greg
how many times through the loop one is?
testhash.each { |d|
puts}
testhash.each_with_index {|d, i| puts i}
Greg H. wrote:
Is there a way to access the count / iteration# within an “each”
loop? i.e.
how many times through the loop one is?
ruby$ ri Enumerable#each_with_index
--------------------------------------------- Enumerable#each_with_index
enum.each_with_index {|obj, i| block } -> enum
Calls block with two arguments, the item and its index, for each
item in enum.
hash = Hash.new
%w(cat dog wombat).each_with_index {|item, index|
hash[item] = index
}
hash #=> {"cat"=>0, "wombat"=>2, "dog"=>1}
excellent - thanks guys
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs