Search code examples
rubyrubygemsnaming

What is the conventional purpose of the "lib" directory in Ruby projects?


Take this ruby gem: https://github.com/ai/autoprefixer-rails

The gem's structure includes a "lib", "spec" and "vendor" directory, and some gemfiles in the root directory.

What exactly is the purpose of directories called "lib" in ruby gems?

It looks like "lib" contains all of the ruby code provided by the gem. So does "lib" mean "the source code files that constitute the library?" (It's hard to say if "lib" means "the library," since arguably the gemfiles are "part of the library," too.) Or does it mean something else? And is the meaning of the directory names in ruby gems defined anywhere? Or is directory naming completely arbitrary?

I am trying to determine if the name "lib" has any special significance as I set out to write my own gem. I'd like to know, because if it doesn't, I might like to pick a different name.


Solution

  • The default "require path," i.e. the directory which will by default be added to $LOAD_PATH when the gem is activated, is "lib". See the rubygems specification:

    REQUIRE_PATHS=(val)

    Paths in the gem to add to $LOAD_PATH when this gem is activated. If you have an extension you do not need to add "ext" to the require path, the extension build process will copy the extension files into “lib” for you.

    The default value is "lib"

    Usage:

    # If all library files are in the root directory...
    spec.require_paths = ['.']
    

    The fact the name is chosen as a default implies it is also a good default naming decision to avoid extra configuration and to help navigate newcomers to the gem.