Prev: win32ole question
Next: Hpricot inner_html
From: Shaz on 7 Apr 2010 21:27 Is it possible to add to the name of a hash while running through an iteration? What I'm trying to achieve: SomeArray.each do |i, name, content| info[i+1] = { "name" => name, "content" => content } end So that I can call, for example info[1] [name] or info[5][content]...
From: Robert Klemme on 8 Apr 2010 03:12 On 08.04.2010 03:27, Shaz wrote: > Is it possible to add to the name of a hash while running through an > iteration? What I'm trying to achieve: > > SomeArray.each do |i, name, content| > info[i+1] = { "name" => name, "content" => content } > end > > So that I can call, for example info[1] [name] or info[5][content]... Make info an Array and just use info << { :name => name, :content => content } Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Brian Candler on 8 Apr 2010 03:44 Is this what you want? SomeArray.each_with_index do |(name,content),i| ... end -- Posted via http://www.ruby-forum.com/.
From: Shaz on 10 Apr 2010 20:40 On 8 Apr, 08:44, Brian Candler <b.cand...(a)pobox.com> wrote: > Is this what you want? > > SomeArray.each_with_index do |(name,content),i| > ... > end > > -- > Posted viahttp://www.ruby-forum.com/. Each with index worked for me - didn't realize each variable in the array has an index that can be called. Thanks!
From: Brian Candler on 11 Apr 2010 03:30
Shaz wrote: > Each with index worked for me - didn't realize each variable in the > array has an index that can be called. Not exactly. The 'each_with_index' method just yields the array index to the block being called. Inside it will be implemented something like this: module Enumerable def each_with_index i = 0 each do |elem| yield elem, i i += 1 end end end But you could consider it like this: class Array def each_with_index size.times do |i| yield self[i], i end end end -- Posted via http://www.ruby-forum.com/. |