Lesson Monday

In this course section, every Ruby project should have the following file structure:

ruby_project
|__lib
  |_ruby_logic.rb
|__spec
  |__ruby_logic_spec.rb
|__Gemfile
|__ruby_script.rb <--- This is optional and only necessary if you plan to have a script.
|__README.md

Gemfile

All projects should always have a Gemfile in the root directory. For now, your Gemfile will look something like this:

source 'https://rubygems.org'

gem 'rspec'
gem 'pry'

These gems are already installed on Epicodus machines, which means you can technically use both rspec and pry without adding and bundling a Gemfile. Ruby will automatically uses the gem versions that are globally installed on the machine if a Gemfile is not present. However, it is important to always have a Gemfile because gem versions may be different (or non-existent) from machine to machine. This way, our projects will have a consistent environment no matter where we work.

Remember that a Gemfile.lock file will be created after bundling your Gemfile. You should never alter this file, though it can sometimes be useful to look at the gem versions listed in the file to debug versioning issues.

Lib and Spec Folders

There should be a lib folder and a spec folder in the root directory of your project. All back-end logic goes in the lib folder and all tests for that logic go in the spec folder.

The naming convention for specs is always the exact name of the logic file with spec appended to it.

Script Files

A shebang is only necessary if you plan to run the file as a script in the terminal. Files in your lib and spec folders should never have a shebang. If a script is needed, place the file in the root directory of your project. In order for your script to access back-end logic from the lib folder, you will need to use require to let the script know where to find that logic. This is similar to requiring the back-end logic file in your specs. However, unlike with RSpec, you will have to tell your script file exactly where to locate the required code.

The beginning of your script file will look something like this:

#!/usr/bin/env ruby

require ('./lib/name_of_ruby_file_goes_here')

The first line is the shebang.

The second line is our require statement. Note that it uses a relative path. A relative path is a path from the current directory to the file we need.

  • The ./ states that we should start our search in this current directory. Our script will not be able to find the file if we don't include this!
  • /lib states that the file will be found in the lib folder.
  • Finally, we have the name of the file, which should be changed from name_of_ruby_file_goes_here to the name of the required file in your project. Note that the require statement doesn't need the .rb extension appended to the end of the file name, but you can add it if you wish.

README

Every project you create should include a README.md file in the root directory regardless of whether it is a Ruby project or not. A README should include all installation instructions as well as directions on using the application. For instance, it should tell the app user how to:

  • cd into the root directory and bundle the project;
  • run tests;
  • run any script files in the root directory.

Verify all necessary steps by cloning your application from GitHub and then documenting your application's setup instructions yourself.

Project File Structure


ruby_project
|__lib
  |_ruby_logic.rb
|__spec
  |__ruby_logic_spec.rb
|__Gemfile
|__ruby_script.rb <--- This is optional and only necessary if you plan to have a script.
|__README.md
  • The naming convention for specs is always the exact name of the logic file with spec appended to it.
  • A shebang is only necessary if you plan to run the file as a script in the terminal.

Sample Gemfile

source 'https://rubygems.org'

gem 'rspec'
gem 'pry'

Terminology

Relative path: The path from the current directory to the file we need.

Lesson 8 of 22
Last updated August 7, 2022