Set up a console for Ruby projects

With IRB Link to heading

  1. Add irb to your Gemfile (yes you need to do this)!
# frozen_string_literal: true

gem 'irb'
  1. Run bundle install of course!

  2. Create a ./bin/console file containing:

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'rubygems'
require 'bundler/setup'
require 'irb'

require_relative '../config/environment.rb'

IRB.start
  1. Be sure you require all your project files in config/environment.rb.

  2. Make your file executable:

chmod +x ./bin/console
  1. Now you can run it like any other program:
./bin/console

# irb(main):001:0>
  1. Optional but fancy: create a Makefile with this content so you can execute make console instead.
.PHONY: console
console:
	@./bin/console
  1. Pro Tip: use load 'path/to/file.rb' to reload file instead of restarting the shell on every edit.

With Pry Link to heading

Step 1. becomes:

# frozen_string_literal: true

gem 'irb'
gem 'pry-byebug'

Yes, you always need irb in your Gemfile!

Step 3. becomes:

#!/usr/bin/env ruby
# frozen_string_literal: true

require 'rubygems'
require 'bundler/setup'
require 'pry'

require_relative '../config/environment.rb'

Pry.start

See also: How to Use: pry-byebug