Ruby Hash inner another Hash is every empty -


i create method group commits of user per month in hash:

def calculate_month_ranking(commits)   rank = hash.new(hash.new(0))     commits.each |commit_yml|     commit = yaml.load commit_yml.data      commit[:commits].each |local_commit|             time = time.parse(local_commit[:timestamp])       month = "#{time.month}/#{time.year}"       rank[month][commit[:user_name]]+= 1       binding.pry # debug     end    end    rank   end 

but rank[month] have value, if call rank, value every empty. why?

[1] pry(main)> rank[month] => {"user1"=>4, "user2"=>1} [2] pry(main)> rank => {} 

your problem in flowing line, assignment not correct hash

      rank[month][commit[:user_name]]+= 1 

i don't know values, adding here example can try out this

declare hash following

rank = {}   

assign values key pair

rank.merge!(:month => {:user_name => 'test' }) 

hear adding console output

2.0.0-p481 :007 > rank = {}   => {}  2.0.0-p481 :008 > rank.merge!(:month => {:user_name => 'test' }) => {:month=>{:user_name=>"test"}}  2.0.0-p481 :009 > rank => {:month=>{:user_name=>"test"}}  2.0.0-p481 :010 > rank[:month] =>  {:user_name=>"test"} 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -