August 03, 2009
A lot of people say that Ruby is a great languages for writing Domain Specific Languages (DSLs). A DSL is a highly abstracted programming language that gives you a natural and intuitive way to deal with a specific logical domain. They can serve as easy flexible APIs for programmers or enable clients to have control over the way a system deals with their business logic. There are lots of examples of DSLs in the Ruby world. Capistrano, RSpec, Thinking Sphinx, Rails’ Routing, just to name a few.
In this post I’ll look at some of the most common ways to create a DSL in Ruby.
A DSL for Defending Medieval Castles
A new client, Medieval Guards, Inc. specializes in guarding medieval castles and fighting off Barbarian attackers.
Tagged with: ruby |
July 09, 2009
I’m a little excited today because some code I wrote was accepted into the Ruby on Rails core.
It’s a patch I wrote to fix a tricky bug I ran into in ActiveRecord which can cause a call to one of your model’s attribute methods to sometimes throw a NoMethodError.
I came across this bug after upgrading a site I help maintain to Rails 2.3. This site allows users to download demos of software. Users have to specify which operating system they want a demo for, and this is stored in a database column named system.
Tagged with: ruby |
June 10, 2009
One of Rails’ selling points is convention over configuration, but most apps do need some configuration. database.yml is a great example.
Here’s some code that loads data into a ruby class from a YAML config file. Just like database.yml the configuration is specific to the rails environment you’re running in.
Tagged with: ruby |
May 19, 2009
Sometimes you want to decide whether or not to pluralize a string based on a quantity. For example, at the bottom of this article should it say 1 comment or 2 comments? It depends on how many comments we have. I was running into this in a lot while pair programming on one project, so Ian Smith-Heisters and I whipped up this simple helper which makes it easy.
class String
def smart_pluralize(num=self)
num.to_i.abs == 1 ? self : pluralize
end
end
Tagged with: ruby |