February 2012
1 post
A Different Form of Ancestry
Okay, this is not really a code-related post. Several months ago I sat down with my son and tried to teach him some good coding concepts. It didn’t last too long because… I had no plan. I didn’t really think much of it until last night.
We were on our way to a soccer practice and talking about things that had gone on during the day. My son is taking an engineering class that...
May 2011
1 post
getting into ancestry
Heading into The Big Rewrite I finally took the opportunity to play with the ancestry gem. Initially developed as a replacement for for acts_as_tree, ancestry has evolved to something much more akin to nested-set support in a tree-like structure. The advances allow you to pluck out a entire subtree or hierarchy path in a single trip to the database. Awesome.
The one thing that I wish we had...
August 2010
1 post
...and?
‘and’ and ‘or’ originate (like so much of Ruby) in Perl. In Perl, they were largely used to modify control flow, similar to the ‘if’ and ‘unless’ statement modifiers. A common Perl idiom is: do_something() or die “It didn’t work!”; The ‘and’ and ‘or’ keywords serve the same purpose in Ruby. Properly...
May 2010
1 post
1 tag
Constant-ly?
I’ve gone back and forth about what should constitute a good use of class-level constants and a recent project has brought this question up once again. In this project there are basically two different uses for class-level constants. The first, and less frequent, use of class-level constants serves to identify “magic values”. The second use is to define a closed set of...
April 2010
1 post
2 tags
RSpec best practices
A few days ago Philippe Creux posted his RSpec Best Practices and Tips. The post is well worth reading even if you prefer another test framework (you are testing, right?!) because there are several tips that are applicable to TDD more broadly than rspec alone. Of all the tips there are two that I cannot re-emphasize strongly enough: “Only One Expectation Per It Block” and...
March 2010
1 post
Water Drop at 2000 Frames per Second →
Water bounces?
February 2010
1 post
Qu’ils mangent de la brioche
The phrase attributed to Marie Antoinette often mistranslated into English as “Let them eat cake” seems particularly appropriate in light of the great post by the folks over at Engine Yard. The “cake” that the French noblewoman referred to was really an enriched bread that the upper classes could dine upon but was generally out of the reach of the starving peasants (all...
January 2010
2 posts
New Year's Cleanup: Compact Is Not Necessarily...
To start the New Year off I am having the distinct pleasure of cleaning up bunch of old code. Aside from my beautiful code (ahem) I am also having the chance to review some other developers and that is leading me to some thoughts about style.
Style Lesson 1: Compact is Not Necessarily Concise
One of the great things about Ruby is it’s concise syntax. Well-written Ruby code is and should...
New Year's Cleanup: Off Color Code
For a number of reasons I have inherited a lot of code recently and I’m in the process of trying to clean up and put the finishing touches on a release we hope to see out the door soon. That’s the good news. The bad news is that my emphasis has had a lot more ‘clean up’ than ‘finishing touches’ of late.
What is Off-Color Code?
One of the things that I have...
December 2009
5 posts
4 tags
Conditionally absolute
I like footers, well, that appear at the foot of the page. I guess that seems obvious enough but what I really mean is a bit more specific. If I have a ‘short’ page in which the page content only extends halfway down the bottom of the browser window (viewport) then I want the footer pushed to the bottom of the viewport. If I have a ‘long’ page where the content scrolls...
1 tag
Spraying for Weeds
What? Really?
Yes, really. It’s the day after Christmas and I spent most of the afternoon spraying for weeds, blowing leaves, and doing other assorted yard cleanup tasks. That probably seems a little bizarre to those of you who get to enjoy the kind of “White Christmas” that is only myth to the Upstate of South Carolina but where a winter day resembles some autumn mornings...
A person can only be a novelty once, and only briefly, and charm, like any commodity, when used uneconomically becomes a wasting asset.
- George Will, 17-Dec-2009
November 2009
2 posts
2 tags
I am the Keymaster, are you the Gatekeeper?
Several weeks ago I started writing an authorization gem on the way home from a weeks’ vacation. The problem with the set of gems and plugins that I’d used in the past were several. One gem was much too tightly coupled to an authentication gem that I no longer used. Others required too much wiring. So, off I went to create the next great gem. At least until I got home and ran...
Thinking Functionally in Ruby →
Tom Stuart introduces Functional Programming ideas to Ruby developers at a London Ruby User group meeting. Great intro to FP with a reference to the most FP-like module in Ruby: Enumerable (and my personal fav Enumerable#inject). Even if FP is not your thing it’s worth watching the video from around the 35 minute mark on to see the Ruby examples.
October 2009
5 posts
2 tags
What's in a name? Inject-ing your code
thoughts.inject(world)?
If you’re new to Ruby you may not realize that the title to the blog is an ode to one of my favorite Ruby idioms: inject. In a nutshell, inject is a customizable accumulator that Ruby mixes into collections through the Enumerable module (e.g., arrays and hashes).
Anatomy of an inject-ion
inject essentially has four parts: a collection to be iterated, a seed value,...
Moving in
After talking with Chris, I decided to move my blog posts from the Upstate Ruby Brigade site to a home of my own (on tumblr). No offense intended to anyone in the Greenville ruby community… just certain factors make it much more worth my while to have my thoughts associated with, well, me.
I think I’ve got all the posts rebuilt with the appropriate dates so (re)enjoy!
Edit — a...
August 2009
1 post
3 tags
Time to clean your filters
The Problem
What do you do when your end user needs to filter? In some cases the answer is easy: provide a custom method for the sort criteria. For example, when you know that your end user wants to know “my jobs” as distinct from “all jobs” a custom ‘mine’ method might work just fine. Don’t worry that the custom routes are not RESTful; they are, they...
March 2009
1 post
3 tags
Globbing up javascript
The Motivation
When you first started with Ruby on Rails you were probably intrigued by the simplicity of adding AJAX to your applications by simply including a line like this:
<%= javascript_include_tag :defaults %>
It was short, concise, and invisible. All the Rails “magic” took over and you didn’t need to worry much about which files were included because everything...
October 2008
2 posts
2 tags
Fixing the Broken Pipes
Why do we need the plumber?
If you’re like me you might slip into a comfort zone and not keep up with all the updates to gems and plugins, especially since github has taken plugin/gem development and forking viral. Usually that’s not much of a problem. Unless, of course, the change is to the gem package manager itself.
I’m not sure exactly what changed. What I know is that...
2 tags
STI Factory revisited
Single Table Inheritance (STI) Revisited
I wrote about Single Table Inheritance (STI) in Rails several months ago. STI is a simple design pattern in which multiple subclasses are stored in a single database table and distinguished by a discriminator column. Rails makes this easy to implement by automatically mapping a column named ‘type’ as the discriminator column (aka,...
June 2008
3 posts
Recently I’ve been using the great Rails 2.1 addition called named_scope to help simplify the process of building queries with meaningful names. Certainly you could do something similar by creating a custom method, but as I wrote earlier, the advantage of named_scope is that the scopes are composable: you can chain them together to build ever more powerful queries.
That’s why...
2 tags
New in Rails 2.1: Timestamped migrations
What was wrong with migrations?
If you’ve been part of any development team that was larger than the “Army of One” you’ve probably run into an issue with migrations. It’s happened to me a few times: one member of the teams goes head-down on some problem and it takes longer than expected. Not wanting to check in ‘broken” code the patch builds up for a...
2 tags
New in Rails 2.1: named_scope
Rails 2.1 Released
If you haven’t heard, the release of Rails 2.1 was announced during core member Jeremy Kemper’s keynote Saturday morning (but it didn’t actually get released until around 2am the next morning).
named_scope
One of my favorite additions to the framework is the absorption of the has_finder plugin into the framework. If you’ve used has_finder in the past...
April 2008
2 posts
2 tags
5 Tips for ActiveResource
The first couple of tips have an indrect impact on ActiveResource. Still, they are worth keeping in mind because they simplify the data with which ActiveResource deals.
Tip 1: Use delegate and :method for encapsulation
If your crash course in Ruby involved reading the Agile book, then the delegate method may be new to you. Delegate is a class-level command that allows you to pass certain method...
2 tags
Why assign site in ActiveResource?
ActiveResource is a great tool for helping your business keep not only its business logic DRY, but even keep its business applications dry. If you’re not familiar with ActiveResource, think of ActiveRecord using an internet-based datastore. It’s a bit more complicated than that but you can do all the basic CRUD methods, custom methods, etc
The advantage that ActiveResource brings,...
March 2008
1 post
2 tags
STI Factory
Single Table Inheritance
One of the abstractions that I really like in Rails is its implementation of Single Table Inheritance (STI). If you’re not familiar with STI, it is a simple design pattern in which you model an inheritance hierarchy in a single database table (Martin Fowler does it more justice here). Since ActiveRecord, Rails’ primary domain modeling base class, is also...
February 2008
3 posts
2 tags
If it quacks like a duck...
If it quacks like a duck…
duck-typing is the Way of Ruby and Rails programming. The phrase attributed to James Whitcomb Riley refers to the preference in Ruby and Rails to be concerned with what an object does (methods and attributes) rather than what an object is (it’s class). That makes a lot of sense from a modeling perspective. We create classes as representations of like...
Objective fun: Even methods can be objects
Even Methods Can Be Objects
One of the great things about Ruby is the extent to which everything is an object. There are significantly fewer ‘primitives’ in Ruby than in any other language with which I’ve worked. Even numbers are objects. Even methods are objects. Well, at least they can be.
Say Hello to My Little Friend Proc
Methods, at least code blocks, can become...
1 tag
Static thinking about a dynamic language
Static thinking about a dynamic language
If you’ve approached Ruby from a background in programming with static (compiled) languages it’s easy to get a lot of “static” in your thinking about Ruby. One of the most common places to lose sight of t dynamic nature of the Ruby language is in class definitions. A class definition is really a call to a method in Kernel that...