Truncating Files in Zsh

Backstory

I was setting up Time Machine on my laptop and figured I should look for big useless files and avoid backing them up. I like GrandPerspective for scanning directories to find disk space hogs. This showed me that I had a bunch of huge Rails logs (from development and testing).

The Syntax

The following command truncates all the log files in all my projects.

: > ~/Projects/*/log/*.log

That colon at the beginning is part of the command.
I think Bash is the same without the colon, but don’t take my word for it.

Postscript

I could have just deleted the files.

rm ~/Projects/*/log/*.log

Rails would simply recreate them as needed.

Don’t Inherit From Struct

I just finished looking through James Edward Gray II’s slides from his talk “10 Things You Didn’t Know Ruby Could Do”.

Number 64 caught my attention because I’ve been writing utility classes that inherit from Struct for years. (Ever since I first saw the pattern in DelayedJob’s README.)

For example, instead of this:

class Name < Struct.new(:first, :last)
  def full
    "#{first} #{last}"
  end
end

You should write this:

Name = Struct.new(:first, :last) do
  def full
    "#{first} #{last}"
  end
end

It’s not really a big deal, but the first example creates a class then immediately subclasses it.  This needlessly leaves an extra class in the hierarchy.  The second example simply assigns the created class directly to your new constant.