【问题标题】:calculating the sum of specific array values in ruby?计算红宝石中特定数组值的总和?
【发布时间】:2012-08-07 16:32:32
【问题描述】:

Pivotal Tracker 是我使用的一个项目管理工具。我正在使用 Pivotal Tracker Ruby Gem 来访问 API。

我正在尝试使用他们分配给他们的点数创建每个成员的哈希值。

最终结果应该类似于:

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

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

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

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

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

任何有关如何修改上述循环以遍历每个故事并对估计值求和(不包括负 1s)的帮助将不胜感激。我觉得有一些不错的 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=[]>] 

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

【问题讨论】:

    标签: ruby arrays hashmap hash-of-hashes pivotaltracker


    【解决方案1】:

    我想这就是你要找的东西:

    for i in 0..@members.length-1
      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 了解详情

    【讨论】:

    • 哇!这似乎可以解决问题!如果你有时间,你能带我经历一些这样我完全理解吗?特别是inject(0) 做什么?还有,最后的: sum 位是什么?
    【解决方案2】:

    只需在此处编写打高尔夫球的代码,但这个 sn-p 也可以解决问题!

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

    我将在 Project 模型中添加 storie_estimates_for(member) 方法,该方法将返回该成员所有故事的估计值数组(当然不是负数!)!

    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 }
    

    返回累加器的最终值。

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 2017-06-15
      • 2019-04-23
      相关资源
      最近更新 更多