I tried rake stats
but that seems highly inaccurate. Perhaps it ignores several directories?

- 296,393
- 112
- 651
- 745

- 108,152
- 195
- 629
- 1,012
5 Answers
I use the free Perl script cloc. Sample usage:
phrogz$ cloc .
180 text files.
180 unique files.
77 files ignored.
http://cloc.sourceforge.net v 1.56 T=1.0 s (104.0 files/s, 19619.0 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Javascript 29 1774 1338 10456
Ruby 61 577 185 4055
CSS 10 118 133 783
HTML 1 13 3 140
DOS Batch 2 6 0 19
Bourne Shell 1 4 0 15
-------------------------------------------------------------------------------
SUM: 104 2492 1659 15468
-------------------------------------------------------------------------------

- 296,393
- 112
- 651
- 745
-
4it seems that it does not support `erb` view files. `cloc app/views`: 100 text files. 96 unique files. 100 files ignored. **Update: `cloc app/views --force-lang=html,erb`** – Mifeng Mar 30 '14 at 17:21
-
@Mifeng Thanks a lot! You can do the same with jade, which also isn't supported out of the box. eg. `cloc example/jadeviews --force-lang=html,jade` – Automatico Dec 03 '14 at 10:50
-
3`apt-get install cloc` for linux – Amit Patel Mar 27 '15 at 06:42
-
1It seems to support erb by now. Even haml works out of the box. – Joe Eifert Nov 02 '16 at 13:35
Here's a simple solution. It counts the lines of code in your rails project's app folder - CSS, Ruby, CoffeeScript, and all. At the root of your project, run this command:
find ./app -type f | xargs cat | wc -l
EDIT
Read the comments. Then try this instead:
find ./app -type f -name "*.rb" | xargs cat | sed "/^\s*\(#\|$\)/d" | wc -l

- 5,363
- 6
- 65
- 108

- 4,635
- 2
- 35
- 21
-
2and several orders of magnitude faster than cloc, if you don't care about the semantics of the lines you're counting – pje Aug 29 '13 at 20:01
-
10I'm really not sure why this got upvoted so much. It simply counts file lines, including blank lines and comments from any source code, configuration files, log files, binary files for crying out loud.That's **very** different from LOC, by any possible definition of what constitutes a line of code. On my current rails project, this reports 35K lines, vs. much more accurate 19K from cloc. – Thilo Sep 11 '14 at 12:27
-
@Thilo, it actually doesn't count config or binary files because it's looking in ./app folder. Though fair enough, there's multiple solutions to everything. – Amin Ariana Nov 26 '14 at 22:45
-
@AminAriana All your assets (and hence images) are under `app/`, so while my comment was a bit too general, it wasn't by much. This will still be inaccurate enough to be meaningless, potentially by an order of magnitude. – Thilo Nov 26 '14 at 23:14
-
3Neat: Added this: " find ./app -type f -name \*.rb | xargs cat | wc -l " to just count ruby code – Daniel Apr 10 '15 at 15:52
-
Added crop blank, spaced or commented out lines: `find ./app -type f -name *.rb | xargs cat | sed "/^\s*\(#\|$\)/d" | wc -l` – Малъ Скрылевъ Aug 30 '17 at 07:26
-
@Thilo: I upvoted this because I do not have to install anything I don't already have to obtain a "close enough" answer for my needs. – Rick Runowski Sep 12 '22 at 17:14
You can try out these two options:
Rakestats snippet from blogpost:
namespace :spec do
desc "Add files that DHH doesn't consider to be 'code' to stats"
task :statsetup do
require 'code_statistics'
class CodeStatistics
alias calculate_statistics_orig calculate_statistics
def calculate_statistics
@pairs.inject({}) do |stats, pair|
if 3 == pair.size
stats[pair.first] = calculate_directory_statistics(pair[1], pair[2]); stats
else
stats[pair.first] = calculate_directory_statistics(pair.last); stats
end
end
end
end
::STATS_DIRECTORIES << ['Views', 'app/views', /\.(rhtml|erb|rb)$/]
::STATS_DIRECTORIES << ['Test Fixtures', 'test/fixtures', /\.yml$/]
::STATS_DIRECTORIES << ['Email Fixtures', 'test/fixtures', /\.txt$/]
# note, I renamed all my rails-generated email fixtures to add .txt
::STATS_DIRECTORIES << ['Static HTML', 'public', /\.html$/]
::STATS_DIRECTORIES << ['Static CSS', 'public', /\.css$/]
# ::STATS_DIRECTORIES << ['Static JS', 'public', /\.js$/]
# prototype is ~5384 LOC all by itself - very hard to filter out
::CodeStatistics::TEST_TYPES << "Test Fixtures"
::CodeStatistics::TEST_TYPES << "Email Fixtures"
end
end
task :stats => "spec:statsetup"
- metric_fu - A Ruby Gem for Easy Metric Report Generation
PS: I haven't tried any of the above, but metric_fu sounds interesting, see the screenshots of the output.

- 38,346
- 37
- 130
- 192
This one calculates number of files, total lines of code, comments, and average LOC per file. It also excludes files inside directories with "vendor" in their name.
Usage:
count_lines('rb')
Code:
def count_lines(ext)
o = 0 # Number of files
n = 0 # Number of lines of code
m = 0 # Number of lines of comments
files = Dir.glob('./**/*.' + ext)
files.each do |f|
next if f.index('vendor')
next if FileTest.directory?(f)
o += 1
i = 0
File.new(f).each_line do |line|
if line.strip[0] == '#'
m += 1
next
end
i += 1
end
n += i
end
puts "#{o.to_s} files."
puts "#{n.to_s} lines of code."
puts "#{(n.to_f/o.to_f).round(2)} LOC/file."
puts "#{m.to_s} lines of comments."
end

- 6,699
- 8
- 48
- 80
If your code is hosted on GitHub, you can use this line count website. Just enter your GitHub URL and wait for the result.
Example for Postgres: https://line-count.herokuapp.com/postgres/postgres
File Type Files Lines of Code Total lines
Text 1336 0 472106
C 1325 1069379 1351222
Perl 182 23917 32443
Shell 5 355 533
...

- 6,955
- 3
- 38
- 69