DEV Community

Cover image for Ruby IRB
Brandon Weygant
Brandon Weygant

Posted on

Ruby IRB

I'm not in such a good enough mood to go and write this with as much attention as it deserves with everything going on in this country right now. A lot of hurt is happening, and a lot of change is necessary.

Anyways, here's a short intro on Ruby's irb command line. This is a useful tool for practicing Ruby and trying new concepts as well ass different tricks.

What is IRB?

IRB - short for "Interactive Ruby" - is a tool to interactively execute Ruby expressions read from the standard input. It's as easy to start an IRB session as typing irb in your shell after installing Ruby.

C:\users\Brandon>irb
irb(main):001:0>
Enter fullscreen mode Exit fullscreen mode

Something like irb(main):001:0> above should appear with the '_' flashing. This means you have successfully entered an irb session.

What Can You Do Inside A Session?

IRB is designed to be a temporary session that you can terminate whenever your ready to start actual work. It's main purpose is to provide a safe place to practice without actually interfering with you program. For example, you can declare a variable like you would in any ruby program:

C:\users\Brandon>irb
irb(main):001:0> x = "Black Lives Matter"
=> "Black Lives Matter"
irb(main):002:0>
Enter fullscreen mode Exit fullscreen mode

You can call the variable any time during your session before you close it simply by typing the variable and pressing enter:

irb(main):002:0> x
=> "Black Lives Matter"
irb(main):003:0>
Enter fullscreen mode Exit fullscreen mode

You can also build functions in irb. This is a bit clunky, as it is designed to be done line by line like this:

b(main):001:0> def injustice
irb(main):002:1> puts "George Floyd, Natosha McDade, Yassin Mohamed, Finan H. Berhe, Sean Reed, Steven Demarco Taylor, Breonna Taylor, Ariane McCree, Terrance Franklin, Miles Hall, Darius Tarver, William Green, Samuel David Mallard, Kwame Jones, Devon Bailey, Christopher Whitfield, Anthony Hill, DeVon Bailey, Eric Logan, Jamarion Robinson, Gregory Hill Jr, JaQuavion Slaton, Ryan Twyman, Brandon Webber, Jimmy Atchison, Willie McCoy, Emantic Fitzgerald Bradford J, Dettrick Griffin, Jemel Roberson, DeAndre Ballard, Botham Shem Jean, Robert Lawrence White, Anthony Lamar Smith, Ramarley Graham, Manuel Loggins Jr, Trayvon Martin, Wendell Allen, Kendrec McDade, Larry Jackson Jr, Jonathan Ferrell, Jordan Baker, Victor White III, Dontre Hamilton, Eric Garner, John Crawford III, Michael Brown, Ezell Ford, Dante Parker, Kajieme Powell, Laquan McDonald, Akai Gurley, Tamir Rice, Rumain Brisbon, Jerame Reid, Charly Keunang, Tony Robinson, Walter Scott, Freddie Gray, Brendon Glenn, Samuel DuBose, Christian Taylor, Jamar Clark, Mario Woods, Quintonio LeGrier, Gregory Gunn, Akiel Denkins, Alton Sterling, Philando Castile, Terrence Sterling, Terence Crutcher, Keith Lamont Scott, Alfred Olango, Jordan Edwards, Stephon Clark, Danny Ray Thomas, DeJuan Guillory, Patrick Harmon, Jonathan Hart, Maurice Granton, Julius Johnson, Jamee Johnson, Michael Dean, and many, many, many others."
irb(main):003:1> end
=> :injustice
Enter fullscreen mode Exit fullscreen mode

And just like with variables we can call that function anytime during our session by typing it in the shell:

irb(main):004:0> injustice
George Floyd, Natosha McDade, Yassin Mohamed, Finan H. Berhe, Sean Reed, Steven Demarco Taylor, Breonna Taylor, Ariane McCree, Terrance Franklin, Miles Hall, Darius Tarver, William Green, Samuel David Mallard, Kwame Jones, Devon Bailey, Christopher Whitfield, Anthony Hill, DeVon Bailey, Eric Logan, Jamarion Robinson, Gregory Hill Jr, JaQuavion Slaton, Ryan Twyman, Brandon Webber, Jimmy Atchison, Willie McCoy, Emantic Fitzgerald Bradford J, Dettrick Griffin, Jemel Roberson, DeAndre Ballard, Botham Shem Jean, Robert Lawrence White, Anthony Lamar Smith, Ramarley Graham, Manuel Loggins Jr, Trayvon Martin, Wendell Allen, Kendrec McDade, Larry Jackson Jr, Jonathan Ferrell, Jordan Baker, Victor White III, Dontre Hamilton, Eric Garner, John Crawford III, Michael Brown, Ezell Ford, Dante Parker, Kajieme Powell, Laquan McDonald, Akai Gurley, Tamir Rice, Rumain Brisbon, Jerame Reid, Charly Keunang, Tony Robinson, Walter Scott, Freddie Gray, Brendon Glenn, Samuel DuBose, Christian Taylor, Jamar Clark, Mario Woods, Quintonio LeGrier, Gregory Gunn, Akiel Denkins, Alton Sterling, Philando Castile, Terrence Sterling, Terence Crutcher, Keith Lamont Scott, Alfred Olango, Jordan Edwards, Stephon Clark, Danny Ray Thomas, DeJuan Guillory, Patrick Harmon, Jonathan Hart, Maurice Granton, Julius Johnson, Jamee Johnson, Michael Dean, and many, many, many others.
=> nil
Enter fullscreen mode Exit fullscreen mode

Don't mind the nil return, its just how it works behind the scenes. The important thing is that we did get the return we were expecting. Functions are clunky as you can see in irb, and it's not the ideal environment (especially if you have complex functions), but it is a good place to work out any kinks or fears you have about functions.

You can end the irb session by typing exit:

irb(main):011:0> exit

C:\users\Brandon>
Enter fullscreen mode Exit fullscreen mode

There is a ton more, like creating new instances of a class - including custom classes. Really, outside of the sometimes clunky interface, irb doesn't have many limitations to what you can't do with Ruby. The important thing to remember is it is a practice tool, and when you terminate the sessions you lose the data you inputted.

Stay safe out there.

Top comments (0)