My blog is moving

February 6th, 2008

I’m changing blog providers, new posts will go to http://squaremasher.blogspot.com/. Update your feeds!

I learned something very important today.

July 5th, 2007

There is no greater validation for a father than the smile of his children.

Spicy recipe to make X11 apps dock-able in OSX

April 26th, 2007

I was a happy programmer when I first launched inkscape from the dock on my Mac Book Pro. Here’s how you do it:

1. Install X11. It is on the system disk that came with your Mac.
2. Install macports or fink.
3. Install The Gimp using the package tool from step 2. Use this to create icons for the dock (.png).
4. Install Platypus, which is a nice little tool that lets you create Mac .app programs for scripts.
5. Create a platypus shell script application and point it at the app you want to run. For instance, this is my script for The Gimp: /usr/bin/open-x11 /opt/local/bin/gimp.
6. Enjoy your creation with a fine wine.

Everything you ever wanted to know about internet acronyms

April 25th, 2007

I _love_ wikipedia. Fun *and* educational.

http://en.wikipedia.org/wiki/List_of_Internet_slang_phrases

A little about continuations

April 9th, 2007

I never really observed the practical application of continuations until I found these little gems. They describe Ruby implementations of a Lisp operator named amb. With examples. Enjoy.

Try this one first.

Then check out this for an object-y implementation.

An friendly overview of continuations can be found here

Goodbye internet radio, you will be missed.

April 1st, 2007

On March 2nd the CRB passed a royalty rate increase that will absolutely destroy small internet broadcasters. If you’re an American and you would like to keep your freedom of choice inform yourself here:

http://www.savenetradio.org/

I also urge you to write to your representative and sign the online petition.

Reflecting on a week in the instructor’s chair

March 9th, 2007

Yesterday at 5 p.m. I finished instructing my first course on Ruby and Rails. I can say it was an amazing experience for all of us. The course that Dave, Andy, Kevin and I put together some 6 months ago is still excellent stuff. It provided the class with many opportunities to look beyond the beaten path and make some discoveries of their own. We re-implemented the in_place_edit_for method to support validations and put some extra sugar to allow single-line declarations for multiple columns on a model. Like this:

  in_place_edit_for :iteration, :name, :start_date, :end_date

That optimization was suggested by a student on the fourth day, pretty impressive. I think it says a lot about the quality of the course and the exceptional students I had.

Now updated for 1.2.2 I know they’re walking away with the most current Rails knowledge we can provide and the Ruby underpinnings necessary to cut their own path through the framework.

I learned quite a lot myself preparing for and during the course. My students are all veterans of other languages and their expertise generated questions that cut straight to the key points of a topic. They really kept me on my toes. Thank you!

I’m eagerly awating my next course. We have quite a few on the drawing board, so it’s anyone’s guess what I’ll be teaching next. I’d like to instruct a pure TDD course eventually. I think I have a good amount of hard-won knowledge to share on that topic. Of course, I love the rails too.

Whatever happens next doesn’t matter much. The future is bright.

Emulating Ruby’s Enumerable in Java

February 1st, 2007

An unfortunate fact of my career is that I still do a lot of consulting work in Java. I developed my sour disposition, as many others before me have, by learning Ruby. One of the features I miss the most in Java land is Ruby’s succinct way of dealing with iteration. For instance, if I wanted to find all the elements in an array of strings containing the word foo I could do:

foos = strings.select {|e| e.match /foo/}

The simple form in Java would require four lines:

List foos = new ArrayList();
for(String e : strings) {
    if(e.contains(“foo”))  foos.add(e);
}

So, I thought, what would be the most succinct way I could implement closures in Java right now? Anonymous classes came to mind, but they’re incredibly verbose for simple operations like the example above. I let the thought percolate a few days which eventually lead me to the beanshell scripting framework. With a little bit of work I was able to allow following in Java:

List foos = rubyCollection.select(“e.contains(\”Foo\”)”);

Beanshell lets you invoke arbitrary snippets of Java at runtime and provide a context for that execution. I was pretty elated with my work until I ventured into some simple performance tests. My first implementation was about 782 times slower than the natural Java version for a 500,000 element list. That killed my buzz. I read up a bit on beanshell and optimized the algorithm down to ~100 times worse than the performance of Java, still far insufficient.

Beanshell is just too slow. I need a parser that can cache an expression, then evaluate it over and over again with different context. If I could, I’d like to do the following in BeanShell:

Interpreter i = new Interpreter();
i.parse(expression);
i.set(“e”, element);
i.eval();
i.set(“e”, element);
i.eval();

I’m going to run through the rest of the Enumerable methods using my current scheme just for fun. Once I’m happy with it I’ll post the code. Maybe someone smarter than I can pick it up where I left off.
Can anyone point me to a parser that will support my needs?

eMusic: a no-brainer for indy & classical fans

November 30th, 2006

The truth is I have eccentric tastes when it comes to music. It is very rare that I haphazardly wander into a disc I really want to buy at a local Mega Electronics Chain®. Even then I’ll only know if I made a good choice when I listen to it, at which point it cannot be returned. For all of these reasons I decided to try a music service. Of these services I chose eMusic, here’s why:

  • No DRM. Plain ‘ol MP3s
  • Quality bitrates ( >192kbps)
  • Works on my linux media server (no client necessary)
  • Cheap: 75 songs a month for $20. That’s about 6 cds.
  • Great selection of jazz, blues, classical, folk, indy rock and many other genres

Using OpenStruct for transient data models

November 29th, 2006

Not too long after I ventured outside the shelter of scaffolding and active record I needed a model to represent transient data. Things that I didn’t plan to persist in the long term (thus, not an ActiveRecord model). For us, this is the form on a search page. This data would never live longer than one request cycle.

The obvious solution was to create a Search class, give it an attribute for each field on our form and define the other methods necessary for it to play nice with the rails framework. Among other things, it would need to be initialize-able from a hash. Frankly, it felt like too much work.

My second thought was to contrive the model as an ActiveRecord and live with a forever empty searches table. This reminded me of the EJB craze and the lessons I learned there. Don’t wear a shoe that doesn’t fit. Frameworks exist to provide a common set of services. If the framework doesn’t provide the thing you need it is always a bad idea to shoehorn it in.

Turns out, Ruby had what we were looking for: OpenStruct. An instance of this class allows you to dynamically define any attribute you want. This is similar to the way Javascript objects behave. Thanks to Dave for pointing that out. Here’s one of our test cases:


def test_change_attribute_value
    search = Search.new
    search.volatile = “once”
	
    assert_equal “once”, search.volatile
	
    search.volatile = “twice”
	
    assert_equal “twice”, search.volatile
end

The “volatile” attribute isn’t declared on the Search class. OpenStruct uses the method_missing hook and a hash to acheive all this magic. Here’s our Search implementation:


require ‘ostruct’
	
class Search < OpenStruct
  def size
    marshal_dump.size
  end
end

As you can see, OpenStruct is providing most of the behavior we want. It can be initialized from a hash, which rails requires. You can access the underlying hash using marshal_dump and marshal_load. Search isn’t done yet, we still need to convert the hash of attributes into a ferret query string, which is trivial. In all, OpenStruct is a very useful construct.

This excercise reminded me why Martin Fowler and friends chose to coin the term POJO. Rails is certainly a much sweeter place than the hell that was EJB 2.0. However, I still need to remind myself that the right way is always the simplest thing that can possibly work. In that spirit, I’ve started naming these outside-the-framework excursions Plain Old Ruby Objects.