Andrew Briening

Cultivating Business With Technology

Ruby Case Statement Without Arguments

1
2
3
4
5
6
7
8
9
10
11
12
def some_method
  false
end

case
when false, nil
   puts "won't print"
when some_method
  puts "still won't print"
when true, or_other_method_that_is_trueish
  puts "There you go."
end

Cool.

Backup Rake Tasks

Rake to the rescue!

rakefile.rb:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'rake'
desc 'rake backup:hoard'
task :default => 'backup:hoard'
namespace :backup do
  desc 'Backup /Users/ dir to the Hoard disk'
  task :hoard => "/Volumes/Hoard" do
    rsync '/Users/Shared/Installed', '/Volumes/Hoard/Software/' and rm_rf Dir['/Users/Shared/Installed/*']
    rsync '/Users', '/Volumes/Hoard/', :options=>'-avuPE --delete'
  end
end
def rsync *args
  options = args.last.is_a?(Hash) ? args.pop : {}
  options = {:options=>"-avuPE", :background=>false}.merge(options)
  cmds = ['sudo rsync', options[:options]] + args
  cmds <<  ' > /dev/null &' if options[:background]
  puts( cmd = cmds.join(' ') )
  IO.popen(cmd) do |f|
    while o = f.gets
      puts o
    end
  end
end

I also made some tasks for syncing my iTunes Library on my old G4 and my Ubuntu machines, but I won’t bore you with those.

The little => "/Volumes/Hoard" makes sure that my Hoard disk is mounted. If it isn’t mounted you get an error like Don't know how to build task '/Volumes/Hoard'. Rsync uses &#92;r to do a progress on each file, but the f.gets in the IO.popen‘s block doesn’t show it. There is probably a way to do this but this is good enough for me.