Irb rails console intro

Introduction to IRB and Rails Console

December 11, 2014|IRB

As a new rails developer you will use IRB and the rails console all the time.

IRB stands for "interactive ruby”. The “interactive” part of IRB is about the speed of processing your request. If your’e in IRB and you type in ruby code and press enter your code is processed immediately. So, if you type in 3.times do puts “Happy Birthday to You!” end it will immediately process it and you’ll see the results. It will look like this:

IRB example of 3.times do

Both the IRB and the rails console is an interactive ruby environment. The difference between IRB and the Rails Console is that when you start up the rails console you load your rails environment.

This is really helpful as you’re learning ruby and rails because you test code quickly to see if it works. When I was first beginning I would use it to make sure I was accessing my models and objects correctly and I would also use it to test out different ways to pull out the information I wanted.

Start Up IRB

To get into IRB go to your command line (if you’re on a mac that’s Terminal). Make sure you have ruby installed. Once it’s open all you have to do is type in irb and push enter. That’s it. Now you’re in IRB. To get into the rails console you should be at the root of your rails app and just type in rails console or rails c.

Use IRB

Once you’re in IRB or Rails Console the process of entering a command is as simple as typing it in, pushing enter, getting the result, and a new promo for another command.

Let’s walk through a simple example. In IRB type in 1 + 1 and push enter. You should see what you already know the answer to be 2.

Simple example or IRB

Everything is now ready for you to type in another command. That’s it, that’s the whole process.

You may want to test out multi-step commands, like this:

Multi-step process in IRB

To do this first type in 3.times do and push enter, then type puts “Happy Birthday to You!” and push enter, and then type end and push enter.

You don’t have to worry about the details of this I just wanted to show you that you can do multi-line commands in IRB and not just simple 1+1 commands.

When You’ll Use IRB

As you learn ruby you’ll spend a lot of time testing ideas and concepts, experimenting, and playing around with Ruby. It’s the perfect place to do that because you can’t really do any damage within IRB. So you should experiment, tweak, test, and just have fun with ruby.

When you’re in the rails console you can save items to the database, but you will be experimenting on a development database so, again, you can’t really do any damage here either.

How to Exit IRB

To leave IRB or the rails console all you have to do is type exit and push enter. Now you’ll leave IRB.

Reloading

If you’re working on a rails project and you enter using rails c or rails console you can simply reload the console. Let me explain what it means to reload. Let’s say you’ve made some changes to your Rails app and you want to see them reflected in the console. There are two ways of doing this.

First, exit and re-enter the rails console. Once you re-enter all your changes will load with it.

Second, and this is my preferred way as it’s faster, is to simply type reload! into IRB and it will reload it as if you were re-entering IRB. So, any changes you made on your app will show up just as if you had just exited and re-entered.

reloading IRB in a rails app (in the rails console)

Conclusion

IRB and the rails console are both the perfect environment to test and learn Ruby.

Want to level up your CSS skills?

Sign up below to receive a CSS tip each week as well as access to my courses and articles as they become available.

I won't send you spam, promise. If it ever feels that way you can unsubscribe at any time.

Sign Up For Free

×

Sign In

×