Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Gibbler: Git-like Hashes and History for Ruby Objects

By Ric Roberts / July 13, 2009

gibblerInspired by Git (the version control system), Delano Mandelbaum has come up with a library called Gibbler, which produces hashes and history for Ruby objects.

Calling the gibbler method on an object will produce a 40 character git-like SHA1 hash (or you can use gibbler.short to get just the first 8 chars).

my_object.gibbler.short # => 4c558a56

Gibbler can also track changes to an object. Every time you call gibbler_commit, it will create a clone of the current object and store it in an instance variable. And just like with git, you can view or revert to any version in the history. By default Gibbler supports history for Hashes, Arrays and Strings.

Get Gibbler and read the documentation on Github. Delano makes the disclaimer that his code is still very new and not particularly efficient (due to keeping everything in memory), but he invites others to play with it and submit patches.

Comments

  1. Hristo Hristov says:

    Actually, the gibbler method returns 20 bytes, which are represented with 40 hexadecimal characters. The same logic for the short version - 4 bytes / 8 hex characters.
    Thanks for sharing, it's nice idea and implementation!

  2. Ric Roberts says:

    Thanks, Hristo. I've edited the article to clear up the length issue.

  3. John says:

    This reminds me of acts_like_git.

    http://github.com/courtenay/acts_like_git/

    That library works pretty much as expected, with one glaring
    exception. When you delete a single object under version control,
    it wipes out the entire repository, so all the other objects'
    revisions are lost.

    I have tried to figure out how to patch it myself, but do
    not understand enough of the git internals.

  4. Simon says:

    What would you use this for?

    My first thought is that it would be a memory hog, since the garbage collector will see everything that has ever existed as referenced, and thus never free up any memory.

    In other words, you would have to be somewhat courageous to use this in a production environment. :)

  5. Delano Mandelbaum says:

    John, thanks for mentioning acts_like_git. I hadn't seen it before. There's also a SHA1 digest implementation in Perl inside KiokuDB:

    http://github.com/nothingmuch/kiokudb/blob/master/lib/KiokuDB/Role/ID/Content.pm

    Simon, I entirely agree with regards to maintaining history in memory. That part is a work in progress and I actually recommend in the readme to not use it in production. Also note that you need to explicitly require 'gibbler/history' so it's straightforward to use just the digests for things like checking when a complex Hash or Array has changed.

  6. Simon says:

    Ah, that's a pretty good use case, I guess. Thanks for the clarification.

  7. Austin Ziegler says:

    The history aspect looks similar in concept to my Transaction::Simple—which can be a memory hog with a complex object graph—with the addition of SHA1 hashes. Aside from object verification, I'm not convinced that the hashes are necessary if you were to support named transactions.

    I haven't gitified the tree yet (it's a straight SVN import from RubyForge), but you can find Transaction::Simple at git://github.com/halostatue/transaction-simple.git under trans-simple/trunk.

Other Posts to Enjoy

Twitter Mentions