Relative Performance of Symbol#to_proc in Popular Ruby Implementations

When Rails 1.1 introduced Symbol#to_proc in 2006, many people, including me, embraced the convenience and elegance of being able to write code such as this:

post_ids = posts.map &:id

instead of the equivalent standard Ruby syntax:

post_ids = posts.map {|post| post.id}

Others pointed out that the Symbol#to_proc approach was much slower than using an explicit block. A commenter on this post on Josh Susser's blog, for example, shows a benchmark revealing Symbol#to_proc to be 3.4 times slower on one example. Continue reading ...

Release Announcement: has_enumeration 1.0.0

This month marks five years since I left a career in embedded systems software development in a quest to reignite my passion for programming. I began my love affair with Ruby shortly thereafter. It was elegant and expressive. It was object-oriented and had closures. It was everything that I had been yearning for while programming in C for all those years.

There are times, though, when I have flashbacks to my C days. Continue reading ...

Arbitrary Reordering of Ruby Arrays

Reordering an array in Ruby is easily accomplished with Array#sort as long as you can provide a block that can compare two elements of the array and indicate their relative ordering. But, what if determining the relative ordering of the elements is not as simple as using the equivalent of the <=> operator on the two elements? What if, say, you wanted to reorder an array of ActiveRecord objects to reflect the order in which their IDs were returned by your search engine? Continue reading ...

Rujubu's View is now Greg's Blog

This blog started its life as Rujubu's View in 2007 when my consulting company, Rujubu Consulting, was actively seeking business. When I relaunched the blog last month after a long period of inactivity (see Relaunching Rujubu's View), I was contemplating returning to active consulting. In preparation for that, I kept the blog's name and its URL on the rujubu.com domain.

Last week I decided to keep Rujubu Consulting on ice for the foreseeable future and accepted a new full-time position at a company (more on that later, perhaps). I am very interested in keeping this blog going, but no longer have a need to promote my consulting business. So, I have renamed it to "Greg's Blog" and have moved it to my personal domain.

Lazing: Lazy Enumerable Methods for Ruby 1.9

The select_first approach I wrote about in Selecting Only What You Need addresses the need for a lazy version of select in a very specific use case. It solved the problem, but left me wanting a general solution.

I found a few blog posts and gems providing lazy versions of a subset of the methods defined in Enumerable. I was unsatisfied with them, though, primarily because of their clunky method names. I strongly believe that code should be easy to read aloud and names like lz_select, lazy_select, and select_lazy do not roll off the tongue. Continue reading ...

Selecting Only What You Need

A couple of weeks ago I was investigating a performance problem. New Relic RPM narrowed the search and code inspection identified the culprit. With the names changed a bit, the problem was:

items = candidates.select {|c| c.super_cool?}.first(5)

It looks innocuous, but, it turns out that super_cool? is expensive to compute and candidates has many members. In the common case where the first five super cool items are found within the first six or seven candidates, a lot of computation is wasted determining whether the other candidates are also super cool. Continue reading ...

Relaunching Rujubu's View

The former incarnation of this blog was stagnate. After more than a year and a half without any new posts, I took it down until I had something new to say. That was preferable to letting it become another neglected site collecting dust on the shelves of the World Wide Web.

As of today, Rujubu's View is back. Continue reading ...