IRB Tweaks

I use Ruby a lot even though our automation is mostly in java. I like to create tools to solve problems (thank you Brian Marick). I developed some tweaks for IRB that I want to share.

Project-Specific History

I use IRB a lot. I like using it to develop because I can try more things to figure them out. I also use it a lot for exploratory testing. The nice part is opening IRB and using the history of my last session to continue on with what I’m doing now. A well-documented, small tweak to .irbrc will allow this.

IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:EVAL_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = “#{ENV[‘HOME’]}/.irb-history

Because I work on many tools for my testing, I often use IRB for various projects, sometimes in the same day. I hate running irb for project B and having to go past the history for project A.

If something annoys me enough, I will try to solve it. Like I found .rvmrc (and other rvm solutions) for storing the ruby versions/gemset in my project directory. Well, this started to annoy me enough. I figured if I could use multiple .irbrc files, then I could use a local .irb_history file. No can do. You either use ~/.irbrc or you use -f and don’t use it. Yuck.

So I found that I could tinker with the .irbrc file. I noticed other people had used code it in. So I decided that I could add ruby code to use an .irb file unique to each directory.

project_dir = Dir.pwd

IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:EVAL_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = “#{project_dir}/.irb_history”

I had problems with this, however. IRB was creating a .irb-history file in my home directory and in the directory I specified. The issue was that everytime I started IRB, it combined the data from both history files, so it was worse than before. I decided that I had to remove the history file in my home dir before I started IRB. I created a function in zsh that did it for me:

rb() {
rm -f ~/.irb-history
irb
}

Now I can have a unique history for each directory that I run IRB from!

p.s. If you know of a fix, I’d be happy to see it. I am trying out pry and that solved the issue! I’ll write more on that later).

Which Project Is This?

Whenever I start an IRB shell, I always know which project that it is in. After a short while, I forget. I have to rename the terminal tab or read the type of data returned from the last call to see which project it’s probably running in. That isn’t good enough for me. So I updated the IRB prompt to show which project that I am in.

project_dir = Dir.pwd
IRB.conf[:PROMPT][:CUSTOM] = {
:PROMPT_N => “#{project_dir.split(“/”).last} >> “,
:PROMPT_I => “#{project_dir.split(“/”).last} > “,
:PROMPT_S => ” : “,
:PROMPT_C => ” > “,
:RETURN => “==> %s\n”
}
IRB.conf[:PROMPT_MODE] = :CUSTOM

As a result, I can tell which project I am in right away:

➜ hello_world git:(master) ✗ rb
hello_world >

Which Gemset is Active?

I like to know which gemset is being used right away in case I forgot to change it – usually I create a .rvmrc file, but sometimes I forget. The result is that I try something but I’m usually in the default ruby (jruby on my system) and default jar.

So I like a reminder of which ruby gems I am using. I added a puts to the .irbrc to show me:

puts “Ruby = #{`rvm list`.split(“\n”).select { |n| n.match /^=/ }[0].split[1]}”
puts “Gemset = #{`rvm gemset list`.split(“\n”).select {|n| n.match /^=>/ }[0].split[1]}”

As a result, I can tell which gems I am using right away:

➜ hello_world git:(master) ✗ rb
Ruby = ruby-2.0.0-p598
Gemset = newby
hello_world >

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s