0

I'm trying to create a dynamic loop within the hash @data below and can't really seem to figure it out. I'm creating an annotatedtimeline-for-rails using the google api from here https://github.com/mcommons/annotatedtimeline-for-rails.

The array within the hash @data has to be dynamic i:e the day number has to be generated by a loop and the name of the product and number are dynamic as well. I'll try to give an example in the loop below

@numdeployed is a number and comes from a table in the db i should be generated by the loop

@data{
    begin loop
    i.day.ago.to_date => { :foo=>@numdeployed, :bar=>@numdeployed, :barbaz=>@numdeployed, :foobar=>@numdeployed },
    end loop
}

The Original Data Hash looks like this

@data = {
    1.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10, :foobar=>40 },
    2.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10,:foobar=>40 },
    3.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10,:foobar=>40 },
    4.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10,:foobar=>40 },
    5.day.ago.to_date => { :foo=>10, :bar=>40, :barbaz=>10,:foobar=>40 }
}

hope someone can help. Thanks

1 Answer 1

3

Are you looking for something like this?

@data = Hash[
    n.times.map do |i|
        [ (i + 1).day.ago.to_date,  { :foo => 10, :bar => 40, :barbaz => 10, :foobar => 40 } ]
    end
]

The n is however many pairs you want in your @data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.