Hacker News new | ask | show | jobs
by amikazmi 4878 days ago
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?