Brad’s Musings

A Smörgåsbord of gray gooey stuff from my brain 
« Back to blog

Ruby DSL Trick with the Hash

Today I had to rig up a method for defining symlinks in Capistrano. I never really liked how the ln command worked to begin with; I think of symlinks in terms of:

Alias A targets File B

To make this easier I wrote up this method:

def link(link)
   source, target = link.keys.first, link.values.first
   run "ln -nfs #{target} #{source}"
 end


Which reads as:

link "alias_a" => "file_b"

I love finding these little ways that make my code more readable.

 

 

Loading mentions Retweet

Comments (3)

Oct 08, 2009
Sam Nardoni said...
I'll be using this. Nice work.
Oct 08, 2009
_dato said...
I'm using this in my Rails template:

def copy(files)
files.each do |from, to|
file(to, File.read(File.join(@template_path, from)))
end
end

copy 'layout.html.erb' => 'app/views/layouts/application.html.erb'

Very nifty.

Oct 08, 2009
grimen said...
DRYer =)

def link(link)
source, target = *link.first
run "ln -nfs #{target} #{source}"
end

Leave a comment...

 
To leave a comment on this posterous, please login by clicking one of the following.
Posterous-login     twitter