Using the Find module to count the LOC of a Rails application
In this article we’re going to explore the following topics:
- use case: counting the LOC of a Rails application
Traversing a directory tree is a pretty common tasks in programming.
In Ruby, the
find library is in charge of handling this task.
It’s a little-known but powerful and user-friendly library.
In this post, we’re going to explore the content of this library and use it for counting the LOC* of a Rails application.
*LOC = Lines Of Code
The find library
find library is part of the Ruby Standard Library.
This means that this library is packaged with Ruby and it’s available when your Ruby setup is completed.
What’s the purpose of this library?
find library aims to support the top-down traversal of a set of file paths.
require this library
Feel free to read the
Requiring a file or library in Rubyif you’re not familiar with the
Kernel#requiremethod in Ruby.
Here we can see that this library defines a new constant named as
This freshly added constant is actually a module
Indeed, we can see that the
Find is an instance of the
Module class — which is what we call a module in Ruby.
Feel free to have a look to the
Modules in Ruby: Part IIarticle if you’re not familiar with the class
Also, it defines 2 module methods:
This method encapsulates the top-down traversal logic of a set of file paths.
Our directory tree
│ └── file2.txt
│ └── file3.txt
2 directories, 3 files
The following script will display the content of this directory tree using the
Here, we call the
Find.find method and we pass the path to our directory tree as argument.
Then we define a block that will be called for each encountered path during the top-down traversal of the
Each of these paths is passed as parameter of the block — the
path parameter in this case.
Note that paths are provided in alphabetical order.
Find.prune method is a kind of “control break” value.
When this method is called within the context of the
Find.find method — so, within the block passed to this method — then 3 operations are processed:
1- the current file or directory is skipped
2- the loop restart with the next entry
3- if the current path is a directory path then the directory is not recursively traversed
Let’s have a look to the following example to illustrate this workflow
Here, when the path points to the
another_directory directory then the
Find.prune method is called.
This operation results in not displaying
In effect, when
Find.prune is called then the directory path is skipped and the associated directory is not recursively traversed.
Counting the lines of code of a Rails application
Before to share the script with you, feel free to have a look to my latest project: RUBYCADEMY.COM
I prepared a script to count the LOC of a Rails application:
This script mainly takes advantage of the
So hope you’ll enjoy it !
Also, don’t hesitate to
star the gist if you find it cool !
Thank you for taking the time to read this post :-)
Feel free to 👏 and share this article if it has been useful for you. 🚀
Also, as I post an article every 3 days, feel free to follow me to be notified of my new article releases.
Here is a link to my previous article: