Walk a Hash with SyncEnumerator

February 15th, 2007

I again was fumbling through PickAxe version 2.0 and stumbled onto SyncEnumerator. One use I see for SyncEnumerator is to walk both the keys and values of a hash, like:

require ‘generator’

ahash = {:five => 5, :six => 6, :seven => 7}
SyncEnumerator.new(ahash.keys, ahash.values).each do |k,v|
  puts "#{k} equals #{v}"
end

The pickaxe is quickly becoming very valuable to me.

require 'pp'

February 13th, 2007

It wasn’t long into my use of ./script/console that I desired a prettier print method. As a gdb user, I was used to ‘set print pretty on’. I spent a few minutes trying to figure out how to do the same in console/irb and failed and moved on. Today I was thumbing through my new copy of PickAxe rev. 2 and stumbled into ‘pp’. Let’s look at the difference:

>> p Task.find(:first)
#<Task:0x314474c @attributes={"job_id"=>"2", "id"=>"1", "description"=>"fill out the unit tests for the existing model and controllers", "duration"=>"80", "started_at"=>"2007-02-02"}>

Not so pretty. Let’s try pp:

>> require ‘pp’
=> ["PP"]
>> pp Task.find(:first)
#<Task:0x3150ba0
 @attributes=
  {"job_id"=>"2",
   "id"=>"1",
   "description"=>
    "fill out the unit tests for the existing model and controllers",
   "duration"=>"80",
   "started_at"=>"2007-02-02"}>

Much better!

update: Colors provided by SubEthaEdit’s fancy “Copy as XHTML”.

I always run autotest (from ZenTest) + redgreen + growl (described here). I highly recommend autotest. But of course, everything has it’s glitches. I just noticed that when I ran ‘rake’ I got a failure, but when I ran autotest all was good. It seems that with autotest, once a fixture is loaded for any test class, that fixture is available to everyone. This isn’t the case with plain old ‘rake’. Once I added the missing fixture, all was better.

Hopefully this helps someone. Minimally, it’s helped me to realize that although I love autotest, a good old ‘rake’ every now and then doesn’t hurt.