Skip to content

Instantly share code, notes, and snippets.

@amicojeko
Last active January 10, 2024 18:29
Show Gist options
  • Save amicojeko/c681ab63144fa6d88c79851215a5286e to your computer and use it in GitHub Desktop.
Save amicojeko/c681ab63144fa6d88c79851215a5286e to your computer and use it in GitHub Desktop.
Finds defined and unused methods in a Ruby project
#!/bin/bash
cd /path/to/my/app # Go to the app path
echo "Searching for Ruby method definitions..."
method_names=$(git grep -h -o -E '^ *def +([a-zA-Z_][a-zA-Z0-9_]*)' -- '*.rb' | sed -E 's/^ *def +//' | sort -u)
if [ -z "$method_names" ]; then
echo "No Ruby method found, or no .rb files found."
exit 1
fi
echo "Method definitions found, searching for their usage..."
for method in $method_names; do
# Coun the number of occurrences of the method name in all the files
count=$(git grep $method -- '*.*' | wc -l | tr -d ' ')
# If it only occurs once, then it's most probably not used
if [ "$count" -eq 1 ]; then
echo "Unused method: $method"
fi
done
echo "Check complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment