Posts for May, 2005

Array#to_h

I found myself writing something like this far too often:

def map_something(array)
  hash = {}
  array.each do |a|
    hash[a] = lookup_value a
  end
  hash
end

It bugged me every time, but digging through the Pick Axe never yielded (ahem) a simpler solution. What I wanted to do is this:

def map_something(array)
  array.to_h do |a|
    lookup_value a
  end
end

But of course you'd need Array#to_h. Here's the cleverest implementation I could think of.

class Array
  def to_h(default=nil)
    Hash[ *inject([]) { |a, value| a.push value, default || yield(value) } ]
  end
end

A pointless example:

a = [1, 2, 3]
a.to_h do |v|
  [v * 2, v * 3]
end
> {1=>[2, 3], 2=>[4, 6], 3=>[6, 9]}

I love Array#inject!

Why PostgreSQL

Or: How I Learned To Stop Worrying And Love The Database.

Timothy Johnson asks in the last post

Can you tell us why the switch from mySQL to postgre? I am not a db-guy (a ui designer) but I would like to hear from a switcher about the differences between the 2. ... Is postgre part of the logical progression? I have made all the same tool changes you have besides this one

I'm not really a "db-guy" either but this is why I switched:

  1. One of my projects needs a really good db with stored procedures. A friend who knows much more than I recommends Postgres.
  2. I dread installing, setting up, and learning yet another application,
  3. But, I download, install from source and have Postgres running in no time at all. Everything just works.
  4. I started looking at the documentation and ended up reading almost the entire thing. I can't explain it but the docs have a great quality. Simple, to the point, but with enough examples to get you going. (ok, I only read sections I-III, and skimmed the rest, but still...)
  5. I tell my wife "this is like, the Ruby of databases!"
  6. I start getting giddy when I find PL/Ruby and the possibility of writing stored procedures in Ruby

Like Ruby, it's the little things that impressed me. Example: having used MySQL for years and not known about the "references" syntax, I'm a little embarassed. But that's the point, Postgres got me to a better place in less time and hassle. Knowing that it's top-notch behind the scenes is icing on the cake, but right now it's more about that good feeling.

That said, I wouldn't really consider Postgres part of the logical progression. MySQL and others are equally good. There's tradeoffs, and I'm sure real db-guys could give you the lowdown.

Btw, if anyone has experience with PL/Ruby or knows where to find more information besides this page, please let me know!

New friends

Little did I know when I jumped headfirst into Ruby that I'd also be switching out just about every tool in the toolbox. It was a huge undertaking all at once, but one I've thoroughly enjoyed. Last week I made another switch, from MySQL to PostgreSQL. This pretty much rounds out the whole spectrum.

I can honestly say I'm happier and more productive now. The time and energy spent learning all this stuff was well worth it. And honestly, it wasn't that bad.

NOMEANSNO!

I figured they were one of those bands I'd just never see. I mean, they've been around since the early 80's. But last night NOMEANSNO put on one of the most amazing shows I've ever seen.

Do not miss these guys. From their performance last night I can't say they're quitting anytime soon, but man they're old. Here's pictures to prove it.

Required reading for today

I really enjoyed reading these, and so might you. First from the ever-entertaining why is Seeing Metaclasses Clearly. If you're into Ruby (and you should be), read this at least twice. Then read (or re-read) Classes and Objects in the Pickaxe. Metaprogramming in Ruby is a joy, and the more I understand the better it gets.

Whether you're into CSS or not, Dave Hyatt's Implementing CSS (Part 1) is a really interesting look at the details of WebKit's implementation. Definitely read more of Dave's posts if you like that kind of thing.

  • May 4, 2005
  • Dealing with links

New cameras are fun

Over the weekend we left Portland to visit my parents in sunny Arizona. No better time to finally get that camera I'd been eyeing, so here's some pictures of the southwest.

Overall I'm really happy with the Canon S410. Great color, small enough, and built like a tank. It's an older model but I actually liked the body better than the newer SD400 style which just seems too itty-bitty.

It's nice to have a camera again. I'm hoping to make this a regular habit and possibly get some Flickr integration happening.