2012-08-07 79 views
0

Pivotal Tracker是我使用的项目管理工具。我使用Pivotal Tracker Ruby Gem来访问API。在ruby中计算特定数组值的总和?

我试图用他们已经分配给他们的点数来创建每个成员的散列。

最终的结果应该是这样的东西:

{"Joe Jones"=>5, "Jane Smith"=>6, "John Doe"=>3} 

我已经在@members创建的每个成员的数组中的项目。

我写了下面的代码来帮助我创建每个成员的散列,并给出他/她已分配的相应故事数量,但我真正需要的是分配给它们的每个故事的estimate值的总和(估计是给定故事的点值)。

#creates a hash of members to number of stories assigned. 
    for i in [email protected] 
     my_hash[@members[i]] = project.stories.all(:owned_by => @members[i], :current_state => ['started', 'unstarted']).length 
    end 

所以,我不知道如何,对于每一个成员,我可以总结一下每个故事owned_by特定成员的estimate值。 注意:估计字段中估计的故事有-1,不应包含在总和中。

任何帮助我如何修改上述循环遍历每个故事和总结估计值(不包括减1)将不胜感激。我有一种感觉,有一些不错的Ruby jujitsu来完成这个任务!通过project.stories.all(:owned_by => @members[i], :current_state => ['started', 'unstarted'])的情况下,人们返回

样本数据琢磨一下格式:

[#<PivotalTracker::Story:0x007f9d6b8 @id=314, @url="http://www.pivotaltracker.com/", @created_at=#<DateTime: 2012-06-18T20:23:42+00:00 ((2456097j,73422s,0n),+0s,2299161j)>, @accepted_at=nil, @project_id=12345, @name="Test ", @description="This is the description for \"Test\"", @story_type="feature", @estimate=5, @current_state="unstarted", @requested_by="joe jones", @owned_by="joe jones", @labels=nil, @jira_id=nil, @jira_url=nil, @other_id=nil, @integration_id=nil, @deadline=nil, @attachments=[]>, #<PivotalTracker::Story:0x007f9d6b8 @id=315, @url="http://www.pivotaltracker.com/", @created_at=#<DateTime: 2012-06-18T20:25:20+00:00 ((2456097j,73520s,0n),+0s,2299161j)>, @accepted_at=nil, @project_id=12345, @name="Test 2", @description="This is the description for \"Test 2\"", @story_type="feature", @estimate=3, @current_state="unstarted", @requested_by="joe jones", @owned_by="joe jones"", @labels=nil, @jira_id=nil, @jira_url=nil, @other_id=nil, @integration_id=nil, @deadline=nil, @attachments=[]>, #<PivotalTracker::Story:0x007f9d6b8 @id=316, @url="http://www.pivotaltracker.com/story/", @created_at=#<DateTime: 2012-06-18T20:25:26+00:00 ((2456097j,73526s,0n),+0s,2299161j)>, @accepted_at=nil, @project_id=12345, @name="Test 3", @description="Description for Test 3 ", @story_type="feature", @estimate=-1, @current_state="started", @requested_by="joe jones", @owned_by="joe jones", @labels=nil, @jira_id=nil, @jira_url=nil, @other_id=nil, @integration_id=nil, @deadline=nil, @attachments=[]>] 

附:如何使这个标题更具描述性的建议将不胜感激。

回答

1

我觉得这里是你在找什么:

for i in [email protected] 
    my_hash[@members[i]] = project.stories.all(:owned_by => @members[i], :current_state => ['started', 'unstarted']).inject(0) do |sum, single_story| 
    single_story.estimate > 0 ? sum + single_story.estimate : sum 
    end 
end 

看一看的文档Enumerable#inject的细节

+0

哇!这似乎是诀窍!如果你有一点时间,你能否引导我了解这一点,所以我完全理解它?特别是'inject(0)'做了什么?另外,那里的':sum'位是什么? – tonic 2012-08-07 17:20:06

0

只是代码在这里打高尔夫球,但这个片段可能做的伎俩以及!

Hash[@members.collect {|member| Array(member, Project.storie_estimates_for(@member).inject(&:+))}] 

我想补充的Project模型storie_estimates_for(会员)方法,将返回的估计(非负当然!)对于这个会员的全部故事的数组!

FWIW,for循环被认为是邪恶的! http://blog.grayproductions.net/articles/the_evils_of_the_for_loop

此外,inject是一种使用累加器将数组简化为单个对象的方法。如果你看一下inject方法的形状:

ary.inject(initial) {|accumulator, element| do_something_with_accumulator_and_element } 

返回累加器的最终值。