Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8118073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:22:19+00:00 2026-06-06T04:22:19+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:22:20+00:00Added an answer on June 6, 2026 at 4:22 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can somebody explain to me whats wrong with the below piece of code ?
Can somebody please explain why the below code returns undefined 2 times ? var
Can somebody explain difference between before and after trigger in oracle 10g with an
Can somebody explain why the following code is not valid? Is it because the
Can somebody explain the main differences between (advantages / disadvantages) the two implementations? For
Can somebody explain me why this T-SQL code only returns one row with the
Can somebody explain me why the small piece of code doesn't work? This is
can somebody explain why is the following trivial code (implementation of Euclid's algorithm to
Can somebody please explain what is wrong in the below applescript: tell the application
Can somebody explain about Iterative Deepening A* ? I still don't understand how it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.