I am trying to add elements to an array, and then push it to a hash. I need following output
{:jan=>"jan", :feb=>"jan", :mar=>"jan", :apr=>"jan", :cont=>["j", "a",
"n"]}
{:jan=>"feb", :feb=>"feb", :mar=>"feb", :apr=>"feb", :cont=>["f", "e",
"b"]}
{:jan=>"mar", :feb=>"mar", :mar=>"mar", :apr=>"mar", :cont=>["m", "a",
"r"]}
{:jan=>"apr", :feb=>"apr", :mar=>"apr", :apr=>"apr", :cont=>["a", "p",
"r"]}
Here is my code:
arr = ['jan', 'feb', 'mar', 'apr']
cont = []
arr.each do |f|
cont.clear
f.split('').each do |t|
cont << t
end
hash = {jan: f, feb: f, mar: f, apr: f, cont: cont}
trending_repos.push(hash)
end
puts trending_repos
In the first iteration, it pushes the array that I want to see at the last. Here is the output:
{:jan=>"jan", :feb=>"jan", :mar=>"jan", :apr=>"jan", :cont=>["a", "p",
"r"]}
{:jan=>"feb", :feb=>"feb", :mar=>"feb", :apr=>"feb", :cont=>["a", "p",
"r"]}
{:jan=>"mar", :feb=>"mar", :mar=>"mar", :apr=>"mar", :cont=>["a", "p",
"r"]}
{:jan=>"apr", :feb=>"apr", :mar=>"apr", :apr=>"apr", :cont=>["a", "p",
"r"]}
consteach iteration. This clears, adds to it, then clear... You to rearrange the order you are doing things.cont.clearI need to movecont = []inside first loop.endfor the inner-loop was the close of the outer block. :P