Wednesday, July 22, 2009

Percent Double Ewe

One of my favorite under-appreciated aspects of the Ruby syntax:


%w{an array of strings} #=> ["an", "array", "of", "strings"]


Two of my favorite uses of this construct:

File::join(%w{who cares what platform}) #=> "who/cares/what/platform"

Kernel::system *%w{sudo cat *} #=> [and the * doesn't glob - WYS is exactly WYG]


Am I the only one who does these things? Or is it just the Rails tide-pool that doesn't?

Thursday, July 16, 2009

Rake and task arguments

I'm putting this together because I really like Rake for the most part, but there is an irritating tendency for the docs to fall out of currency. And they like the #nodoc tag. A lot.

So, in more recent versions of Rake (0.8.6 or thereabouts), you can use arguments to tasks, like this:


task :my_task, [:whick, :whack] => [:depend_on] do |task, args|
args.with_defaults(:whack => 42)
end


(Actually, there's about a dozen (well, four) different formats for task definition, but there's 20 reasonable formats that won't work, and I've found that the :task, [:args] => [:deps] format is the best one to remember.)

Now, if a dependency also has arguments, and they're a subset of the primary tasks arguments, then they'll be assigned, regardless of their relative order.

task :depend_on, [:whack] do; end

:depend_on will get :whack assigned from the arguments assigned by :my_task.