Useful Ruby Gems

April 25, 2022   • ruby

Although Ruby on Rails provides great defaults out-of-the-box, you can easily extend Rails using Ruby gems. Here are eight gems that I find myself using every time I start a new Rails project.

Semantic Logger

Semantic Logger is a feature rich logging framework, and replacement for existing Ruby & Rails loggers.

gem "amazing_print"
gem "rails_semantic_logger"

Better Errors

Provides a better error page for Rails and other Rack apps. Use with binding_of_caller to include source code inspection, a live REPL and local/instance variable inspection for all stack frames.

group :development do
  gem "better_errors"
  gem "binding_of_caller"
end

Rails-dotenv

Provides a simple way to load environment variables from .env.

gem 'dotenv-rails', groups: [:development, :test]
  • Add your application configuration to your .env file in the root of your project (git ignore it):
S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
  • Whenever your application loads, these variables will be available in ENV:
config.fog_directory  = ENV['S3_BUCKET']

Debugger

Enable step-by-step debugging and stack navigation in Pry.

gem 'pry-byebug'
  • Set up the shortcuts
touch ~/.pryrc
➜  code ~/.pryrc

if defined?(PryByebug)
    Pry.commands.alias_command 'c', 'continue'
    Pry.commands.alias_command 's', 'step'
    Pry.commands.alias_command 'n', 'next'
    Pry.commands.alias_command 'f', 'finish'
end

Annotate_Models

Annotate Rails classes with schema and routes info.

group :development do
  gem 'annotate'
end
  • Annotate the models
cd ~/projects/writer
annotate

RSpec-Rails

RSpec for Rails. Behaviour Driven Development.

group :development, :test do
  gem 'rspec-rails', '~> 5.0.0'
end

FactoryBot-Rails

A library for setting up Ruby objects as test data.

group :development, :test do
  gem "factory_bot_rails", "~> 6.2"
end
  • Read the Getting Started guide.
  • Create one factory for each class that provides the simplest set of attributes necessary to create an instance of that class.
  • If you’re creating ActiveRecord objects, that means that you should only provide attributes that are required through validations and that do not have defaults.
  • Other factories can be created through inheritance to cover common scenarios for each class.

Rubocop

A Ruby static code analyzer and formatter, based on the community Ruby style guide.

group :development do
  gem 'rubocop', require: false
end
  • Add a config file named .rubocop.yml
  • Disable certain checks
Style/StringLiterals:
  Enabled: false

Here’s the resulting Gemfile with a short description of each gem.

# Semantic Logger is a feature rich logging framework, and replacement for existing Ruby & Rails loggers.
gem "amazing_print"
gem "rails_semantic_logger"

group :development, :test do
  # Step-by-step debugging and stack navigation in Pry
  gem 'pry-byebug'

  # A Ruby gem to load environment variables from `.env`.
  gem 'dotenv-rails', groups: [:development, :test]

  # # RSpec for Rails. Behaviour Driven Development.
  gem 'rspec-rails', '~> 5.0.0'

  # A library for setting up Ruby objects as test data.
  gem "factory_bot_rails", "~> 6.2"
end

group :development do
  # Use console on exceptions pages [https://github.com/rails/web-console]
  gem "web-console"

  # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
  # gem "rack-mini-profiler"

  # Speed up commands on slow machines / big apps [https://github.com/rails/spring]
  # gem "spring"

  # Provides a better error page for Rails and other Rack apps.
  gem "better_errors"

  # Includes source code inspection, a live REPL and local/instance variable inspection for all stack frames.
  gem "binding_of_caller"

  # Annotate Rails classes with schema and routes info
  gem 'annotate'

  # A Ruby static code analyzer and formatter, based on the community Ruby style guide.
  gem 'rubocop', require: false
end