Archive for July, 2008
Using Hash#values
This is minor, but I thought it was interesting nonetheless.
I needed to iterate through the values of a Hash. So I called .values on the Hash in question. What was interesting about this had nothing to do with the results of calling this method, but instead what it was actually doing to the Hash.
>> myhash = { :first => "one", :second => "two", :third => "three" } => {:second=>"two", :first=>"one", :third=>"three"}
The myhash how has a class of Hash, which is to be expected, since that's what I gave it.
>> myhash.class => Hash
When .values is called, it returns all of the values in the Hash.
>> myhash.values => ["one", "two", "three"]
You may have noticed this in that last example, but when .values was called on that Hash, the class of the result changed altogether. Let's examine the class of this Hash while we are calling .values on it.
>> myhash.values.class => Array
So calling .values on the Hash literally built an Array with the values from the preceding Hash!
Read more about Hash#values in the Ruby Documentation.
Git Aliases
There has been talk about how to shorten Git commands, mostly in an effort to assist those of us who are coming from Subversion. One proposed solution has been to use the alias mechanism native to your shell environment.
Another solution does exist. This one is internal to git itself. Due to that fact, the two main benefits are a shorter load time when starting a new shell (since you're not dealing with alias entries in your .profile script) and easier git-centric configuration.
Simply add the following to ~/.gitconfig:
[alias] co = checkout st = status br = branch ba = branch -a
Now instead of typing git checkout branch you only need to type git co branch. You can customized these as much as you'd like, adding other commands as well, as long as the command after the equals sign is a valid git command.
The obvious down side to this method is that you have to type git before these aliases. Using the other method, you do not.