News for hackers. Stuff that matters.
Re:Ask HN: Why do so many people hate Ruby? by amikazmi on Sunday February 03, 2013 @06:12AM (#5159344)
I understand you.. this Ruby code is unclear.

There is a lot of bad code out there, but you shouldn't judge Ruby by a random code snippet- Ruby is just the tool, and the developer is responsible to write clean code.

I don't know what the code is trying to do with all the nested mapping.. it seems like the first 2 lines break encapsulation of the Repo model, trying to gather all the commits. This is unrelated to Ruby, it's a "bad" OO design (might be wrong, I didn't read the rest of the code)

So lets ignore that, and just rewrite from line 3 and below:

    def fresh_commits(repo, n = 10)
      commits = repo.heads.map do |h|
        repo.commits(h.name, n).map { |c| Commit.new(c, h) }
      end

      commits.flatten.uniq_by(&:id).sort_by(&:committed_date).first(n)
    end

Do you agree that the last line is clean & readable, even if you don't know "exactly" what "&:function_name" does?

There are no comments on this comment yet.