Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to incorporate Interactive Ruby into my development process?

I am trying to find a better way to integrate IRB with my normal ruby devleopment. Currently I rarely use IRB with my code. I only use it to verify syntax or to try something small.

I know I can load my own code into ruby as a

require 'mycode'

but this usually doesn't mesh with my programming style. Sometimes the variables I want to examine are out of scope or inside of a loop. Is there an easy way to fire up my script and freeze at a certain point inside of IRB? I guess I'm looking for an easier way to debug my ruby code without breaking my F5(compile) key.

Maybe a more experienced ruby developer can share with me a more streamlined method of development.

like image 973
Jeremy Mack Avatar asked Sep 27 '08 17:09

Jeremy Mack


People also ask

How do I use IRB in Ruby?

To use it, you launch the irb executable and type your Ruby code at the prompt. IRB evaluates the code you type and displays the results. IRB gives you access to all of Ruby's built-in features, as well as any libraries or gems you've installed.

What does IRB stand for Ruby?

IRB stands for “interactive ruby” and is a tool to interactively execute ruby expressions read from the standard input. The irb command from your shell will start the interpreter.

How do I run a Ruby script in debug mode?

In order to start the Ruby debugger, load the debug library using the command-line option -r debug. The debugger stops before the first line of executable code and asks for the input of user commands.

How do you use pry in Ruby?

The Ruby programmer can invoke the pry console during runtime by inserting the line 'binding. pry' wherever they would like to stop the program. When the interpreter hits the binding. pry, Pry will open a REPL session in the console, allowing you to test variables, return values, iterations, and more.


3 Answers

Install the ruby-debug gem. Of course, require it inside your app (only in development/test mode). Now you can write 'debugger' where you want to stop execution.

Once your app stop at your breakpoint, you can type 'help' to know about all commands. One of them is 'irb'. It starts an IRB session in which you have access to all methods in your current context.

I personally mostly use p (print), eval, v i (instance vars) and v l (local vars). Of course, n for next and c for continue.

The command to step out of a given block/method never worked for me though. I never investigated why :-)

like image 198
webmat Avatar answered Sep 19 '22 00:09

webmat


I don't tend to use irb directly that frequently, as I tend to be inside rails and so use script/console a bunch, but I do like using the ruby debugger (Ruby Debug gem). It lets you set a breakpoint basically and then step through your code line by line.

Here's a screencast about it that I haven't actually watched, but a quick search pulled it up, and it could be useful:

http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/

like image 24
Cameron Booth Avatar answered Sep 19 '22 00:09

Cameron Booth


For Ruby development in Eclipse: there's a much improved version of RDT (ruby development tools) available now. To install it directly in Eclipse, click Help > Software Updates > Find and Install > Search for new features radio button > next > new remote Site > Name = Ruby and URL = http://update.aptana.com/update/rdt/3.2/

Another Ruby plugin is the shiny new Eclipse DLTK (dynamic languages toolkit). DLTK stable release 1.0.M5 just came out a few days ago. Here are some useful installation tips.

like image 20
user64842 Avatar answered Sep 20 '22 00:09

user64842