Вопрос

Can somebody explain to me what the below code is doing. before and after are hashes.

def differences(before, after)
    before.diff(after).keys.sort.inject([]) do |diffs, k|
        diff = { :attribute => k, :before => before[k], :after => after[k] }
        diffs << diff; diffs
    end
end

It is from the papertrail differ gem.

Это было полезно?

Решение

It's dense code, no question. So, as you say before and after are hash(-like?) objects that are handed into the method as parameters. Calling before.diff(after) returns another hash, which then immediately has .keys called on it. That returns all the keys in the hash that diff returned. The keys are returned as an array, which is then immediately sorted.

Then we get to the most complex/dense bit. Using inject on that sorted array of keys, the method builds up an array (called diffs inside the inject block) which will be the return value of the inject method.

That array is made up of records of differences. Each record is a hash - built up by taking one key from the sorted array of keys from the before.diff(after) return value. These hashes store the attribute that's being diffed, what it looked like in the before hash and what it looks like in the after hash.

So, in a nutshell, the method gets a bunch of differences between two hashes and collects them up in an array of hashes. That array of hashes is the final return value of the method.

Note: inject can be, and often is, much, much simpler than this. Usually it's used to simply reduce a collection of values to one result, by applying one operation over and over again and storing the results in an accumlator. You may know inject as reduce from other languages; reduce is an alias for inject in Ruby. Here's a much simpler example of inject:

[1,2,3,4].inject(0) do |sum, number|
  sum + number
end
# => 10

0 is the accumulator - the initial value. In the pair |sum, number|, sum will be the accumulator and number will be each number in the array, one after the other. What inject does is add 1 to 0, store the result in sum for the next round, add 2 to sum, store the result in sum again and so on. The single final value of the accumulator sum will be the return value. Here 10. The added complexity in your example is that the accumulator is different in kind from the values inside the block. That's less common, but not bad or unidiomatic. (Edit: Andrew Marshall makes the good point that maybe it is bad. See his comment on the original question. And @tokland points out that the inject here is just a very over-complex alternative for map. It is bad.) See the article I linked to in the comments to your question for more examples of inject.

Edit: As @tokland points out in a few comments, the code seems to need just a straightforward map. It would read much easier then.

def differences(before, after)
  before.diff(after).keys.sort.map do |k|
    { :attribute => k, :before => before[k], :after => after[k] }
  end
end

I was too focused on explaining what the code was doing. I didn't even think of how to simplify it.

Другие советы

It finds the entries in before and after that differ according to the underlying objects, then builds up a list of those differences in a more convenient format.

before.diff(after) finds the entries that differ.

keys.sort gives you the keys (of the map of differences) in sorted order

inject([]) is like map, but starts with diffs initialized to an empty array.

The block creates a diff line (a hash) for each of these differences, and then appends it to diffs.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top