2006-09-01

Ruby Operator Gotcha

I’m sure this is well-documented somewhere, but it tripped me up anyway.

As you may know, Ruby has operators “or” and ”||”, “and” and “&&”. In normal logic operations they appear to be interchangeable. However, there are differences that may bite you if you use the wrong one.

Let’s say you want to coalesce two values—that is, take the first value if it isn’t nil or false, otherwise the second value. Let a = nil and b = "foo". If you write c = a or b, you might expect c to be “foo”, but you would be mistaken.

It’s probably operator precedence, because c = (a or b) gets you what you want. If you prefer to lose visually-redundant parenthesis, c = a || b is your friend.

“and” and “&&” work similarly.

2006-07-07

Death to MySQL

I despise the influence MySQL has had on a whole generation of developers. DHH considers databases to be little more than a glorified hash table. While I don’t like to embed too much of an application into the database and generally eschew stored procedures when they’re not absolutely necessary, I consider database constraints to be as vital as a constitution and integral to keeping the entire system DRY (Don’t Repeat Yourself—Every piece of knowledge must have a single, unambiguous, authoritative representation within a system).

When I added constraints to a table representing an hierarchical account structure, I was shocked to discover that Rails was not loading my test fixtures in sequential order. That’s right, Rails was taking my carefully-ordered YAML file with records ordered such that parent records should always be loaded before their children and attempting to insert records from the middle first, which the database didn’t appreciate due to the constraints I had established.

I can only surmise that Rails is loading the entire YAML file into a hash table in memory before inserting its values—which we all know are not stored sequentially—into the database. This method does not scale to a significant number of records and it is a slap in the face to those who believe in database-enforced referential integrity.

I blame MySQL because its fans turn its vices into virtues and their mindset affects their software in ways that violate the principle of least surprise.

2006-06-12

Excel and Ruby

Two libraries that look to be handy: One for creating an Excel spreadsheet (without “benefit” of OLE): http://rubyforge.org/projects/spreadsheet, the other for parsing Excel spreadsheets: http://raa.ruby-lang.org/project/parseexcel/.

2006-06-01

Efficiency

As you’re coding in a language, particularly a high-level language like Ruby, you may worry about the efficiency of various operations—particularly if you were once a DOS programmer working on extremely-limited machines at one time.

For example, the Ruby Array type has push, pop and shift operations. Push and pop add an element to the end of the array and remove it, respectively, of course. As you might expect, the capacity of the array is increased in blocks large enough so that the vector does not need to be reallocated for every push. As you might also expect, popping an element off the array merely retrieves the value and decrements the length.

However, what about shift, which pops an element off the beginning of the array? I was pleasantly surprised to find that Ruby does the equivalent of a pop—after retrieving the element, it increments the pointer and decrements the length.

The only operation of the set with performance implications is unshift, which prepends an element to the array. This operation always involves moving a block off memory, which I’m sure is fairly optimized, but it is still an O(n) rather than an O(1) operation.

Flow Control

Get with the Flow. Are you happy?

2006-05-30

REPL

Who needs an IDE when you have a REPL?

Okay, so maybe I still like IDE’s, but if you catch yourself writing too much code before you try any of it, maybe you need to set up a REPL environment. I like writing code with unit tests because then it doesn’t have to reach critical mass, with sixteen cylinders all firing at once, before I see positive results for my effort.

2006-05-09

Pachelbel Rocks

This video is quite impressive—a must-see if you like both classical and rock.

2006-05-01

has_many :through trick

Save yourself some time. When using has_many :through, first declare a normal has_many relationship with the rich join table. Otherwise, you’ll be pondering the meaning of life, the universe and HasManyThroughAssociationNotFoundError.