Skip to main content

TIL: Claude Code Hooks Can Auto-Fix RuboCop Violations

1 min read

Today I learned Claude Code has hooks! These let you automatically run commands when certain events happen. I'm using this to auto-fix RuboCop violations every time Claude edits a Ruby file, and it's making my development flow so much smoother.

Working on my OctoPrint gem, Claude was inconsistent about running RuboCop after making changes. Sometimes it would run, sometimes it wouldn't, and it often decided to manually fix style violations instead of using the autocorrections.

While reading the Claude Code hooks documentation, I realized I could automate this completely. Hooks execute shell commands in response to events like file edits, tool usage, or when Claude finishes responding.

Example

I added this hook to my ~/.claude/settings.json:

{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|MultiEdit|Write",
"hooks": [
{
"type": "command",
"command": "file_path=$(echo '$TOOL_PARAMETERS' | jq -r '.file_path // empty'); if [[ $file_path == *.rb ]] && [[ -f $file_path ]]; then rubocop \"$file_path\" -a; fi"
}
]
}
]
}
}