Boosting Your Rails Console With .irbrc

You’re likely familiar with the IRB (Interactive Ruby Shell) console, a powerful tool that enables you to quickly test and experiment with Ruby code. However, did you know that you can customize the IRB console by using a .irbrc file? This file is a Ruby script that executes every time a new IRB console is started. By using a .irbrc file, you can customize the IRB console, improve your productivity, and ensure consistency across your development environment.

In this article, we’ll explore .irbrc file through examples of code that you can use on your own.

You can customize the IRB console to suit your preferences and work style. For example, you can set the prompt to display additional information, load custom libraries or modules, and add custom methods and functions that you frequently use.

Creating a custom prompt

For example, you can display the name of a hypothetical application (i.e. “my-app”) before the standard IRB prompt. This can help to differentiate the IRB console for different projects or environments.

1
2
3
4
5
6
7
8
9
require 'irb/completion'
    
class MyPrompt < IRB::Context::SimplePrompt
  def prompt(*args)
    "[my-app] #{super}"
  end
end
    
IRB.conf[:PROMPT_MODE] = MyPrompt.new
1
2
3
4
[my-app] irb(main):001:0> variable_name = "Hello, world!"
[my-app] irb(main):002:0> puts variable_name
Hello, world!
=> nil

Set up awesome_print gem to enable colored and formatted output in the console. With this gem, you can easily distinguish between different types of objects and their attributes, making it easier to read and understand the output of your console commands.

1
2
require 'awesome_print'
AwesomePrint.irb!
1
sample_object = { name: 'John Doe', age: 30, profession: 'Developer' }

The output will be:

1
2
3
4
5
{
  :name => "John Doe",
  :age => 30,
  :profession => "Developer"
}

Define custom methods

For example, if you frequently need to calculate the average of an array of numbers, you can define a mean method like this:

1
2
3
def mean(array)
  array.reduce(:+) / array.size.to_f
end

Now you can use the mean method in the console:

1
2
irb(main):001:0> mean([1, 2, 3, 4, 5])
=> 3.0

This custom module MyIRBUtils adds two convenience methods to the IRB console: clear and reload!. This can be useful when experimenting with code changes in the IRB console.

1
2
3
4
5
6
7
8
9
10
11
module MyIRBUtils
  def clear
    system('clear') || system('cls')
  end
  
  def reload!
    load $0
  end
end
    
include MyIRBUtils

This Ruby on Rails code defines a method called clear_database, which is used to delete all records from all tables in the connected database. Additionally, the code defines an alias method c for clear_database, which allows the user to call the method using either name.

1
2
3
4
5
6
7
def clear_database
  ActiveRecord::Base.connection.tables.each do |table|
    ActiveRecord::Base.connection.execute("DELETE FROM #{table}")
  end
end

alias c clear_database

Set default configurations

For example, if you prefer to use 4 spaces for indentation, you can add this line to your .irbrc file:

1
IRB.conf[:INDENT] = 4

Use Pry instead of IRB. Pry is an alternative to IRB that provides additional features like syntax highlighting, better navigation, and a more powerful shell. To use Pry instead of IRB in the Rails console, add this line to your .irbrc file:

1
2
require 'pry'
Pry.start

Loading custom scripts, gems or configurations:

This code loads the Dotenv library to load environment variables, loads the AWS SDK for S3, and sets up the AWS configuration with environment variables.

1
2
3
4
5
6
7
8
9
require 'dotenv/load'
require 'aws-sdk-s3'
    
Aws.config.update({
  region: ENV['AWS_REGION'],
  credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])
})
    
# ... other custom setup code

I hope these examples give you a better idea of the kind of code that you can include in your .irbrc file to customize the IRB console and improve your development experience!