Friday, August 3, 2012

[Rails] Re: dynamic loop in a hash

This should work: 

items = 12.times.map { [i.day.ago.to_date, { :coder=>@numdeployed, :imedidata=>@numdeployed, 
:balance=>@numdeployed, :CTMS=>@numdeployed } ] }.flatten
result = Hash[*items]

Daniel

On Friday, August 3, 2012 5:03:04 PM UTC-4, Ruby-Forum.com User wrote:
I'm trying to create a dynamic loop within the hash @data below and
can't really seem to figure it out. The array within the hash has to be
dynamic i:e the day number has to be generated by a loop either while or
until and the name of the product  and number are dynamic as well. I'll
try to explain in the loop below

@numdeployed comes from a table in the db

i = 0
until i == 12
  i.day.ago.to_date => { :coder=>@numdeployed, :imedidata=>@numdeployed,
:balance=>@numdeployed, :CTMS=>@numdeployed },
  i += 1
end


Original Data Hash

@data = {
      1.day.ago.to_date => { :coder=>10, :imedidata=>40, :balance=>10,
:CTMS=>40 },
      2.day.ago.to_date => { :coder=>10, :imedidata=>40, :balance=>10,
:CTMS=>40 },
      3.day.ago.to_date => { :coder=>10, :imedidata=>40, :balance=>10,
:CTMS=>40 },
      4.day.ago.to_date => { :coder=>10, :imedidata=>40, :balance=>10,
:CTMS=>40 },
      5.day.ago.to_date => { :coder=>10, :imedidata=>40, :balance=>10,
:CTMS=>40 },
      6.day.ago.to_date => { :coder=>10, :imedidata=>40, :balance=>10,
:CTMS=>40 },
      7.day.ago.to_date => { :coder=>10, :imedidata=>40, :balance=>10,
:CTMS=>40 },
      8.day.ago.to_date => { :coder=>10, :imedidata=>40, :balance=>10,
:CTMS=>40 },
      9.day.ago.to_date => { :coder=>10, :imedidata=>40, :balance=>10,
:CTMS=>40 },
     10.day.ago.to_date => { :coder=>200, :imedidata=>190, :balance=>70,
:CTMS=>90 },
      11.day.ago.to_date => { :coder=>190, :imedidata=>200,
:balance=>200, :CTMS=>150 },
      12.day.ago.to_date => { :coder=>100, :imedidata=>400,
:balance=>100, :CTMS=>100 }

}

hope someone can help. Thanks

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ZhiN7kFGTKAJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate