Buy @ Amazon

Search This Blog

November 19, 2010

Ordered Hashes in Ruby

Today, while playing a story in my web app development project, I felt the need to have an ordered hash in Ruby 1.8.7. Argh, this version of Ruby doesn't have ordered hash at all! Surprisingly enough, JRuby versions 1.3.1 or 1.5.1 preserves insertion order.

The way Ruby 1.8.7 hash behaves:


The way Ruby 1.9.1 hash behaves:

The way JRuby 1.3.1 hash behaves:


Coming back to our problem, how do I do have something like Ordered Hash data structure with Ruby 1.8.7? There are several libraries that either extend or replace Ruby's Hash. OrderedHash and RubyFacets being some of the famous ones. However, if you are developing a web application using Rails, then you have to search no where - just use Rails' ActiveSupport::OrderedHash. All you need to use it is
require 'active_support/ordered_hash' in your class/module

With Ruby 1.9, this wouldn't be a problem because Ruby 1.9 preserves the insertion order into the data structure. This only gives me foolish happiness for a second or two, after which my academic reasoning onsets bringing sinking unhappiness for the Ruby 1.9 preserving hash ordering by default. Should my prediction go right (or wish come true???) this will revert back to the way it was in Ruby 1.8.* and to solve this problem of having an ordered hash data structure, Ruby future version might have something like OrderedHash, the way Rails has one.

SIDE NOTE: A hash (table/map) is a data structure for storing items in key/value pairs using a hash function. Ideally, the hash function guarantees to map each key to a unique slot index. With this data structure, the average cost of lookup/insertion/deletion for an element remains constant irrespective of the number of elements that this data structure holds. Understandably for this reason this data structure is used in many softwares like databases, caches.


References:
1. http://en.wikipedia.org/wiki/Hash_table
2. http://www.icoretech.org/2009/08/ruby-ordered-hash-tip-with-rails/
3. http://loopkid.net/articles/2008/06/19/ordered-hashes-in-ruby-1-8
4. http://www.markhneedham.com/blog/2010/09/07/ruby-hash-ordering/

Recommended Books:

The Ruby Programming Language      Programming Ruby 1.9: The Pragmatic Programmers' Guide (Facets of Ruby)     The Well-Grounded Rubyist

November 9, 2010

Git-SVN bridge

This post should serve as a quick reference material for Git-ites (wannabes and me) who'll have to use svn as configuration management system for whatever reason it be. In such situations install git-svn onto your system so that you can have git as your local repository and pull from/push to remote svn that the team uses as central versioning system.

Remember that for all actions on your local repository use the regular git commands. The need for git-svn commands arises only when you need some information/artifacts from svn repository.

1. What is the command to update my local git repo with changes from the remote svn repo?
    git svn rebase

2. What is the command to commit my local changes to svn repository?
     git svn dcommit

3. What is the sequence of steps to push my local changes to svn repo?
     i. When you change existing or add new files, you need to add it to the local git repo
         git add <file_path>  or
         git add <directory_path>
     ii. Commit added files to local git repo
          git commit -m "<Commit Comment>"
     iii. Get latest updates from svn repo
          git svn rebase
     iv. Commit changes to svn repo
          git svn dcommit

4. What is the command to see log of commits?
    git svn log --limit=5 --verbose --show-commit
    where,
    --verbose or -v
        it’s not completely compatible with the --verbose output in svn log, but reasonably close.
    --revision=<n>[:<n>]  or -r
         is supported, non-numeric args are not: HEAD, NEXT, BASE, PREV, etc

[More to come...]


Recommended Books for Git:
Pro Git     Version Control with Git: Powerful Tools and Techniques for Collaborative Software Development