Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm trying to learn how to step through Ruby code (written by more experienced programmers) in order to improve my knowledge of Ruby. Problem is that I don't really know how to do it well. Googling the topic brought me to an about.com page on Ruby; I thought a higher quality more comprehensive answer should belong on StackOverflow, so if anyone can write an answer (with an example) for it, showing how a beginner can step through code to learn as much as possible about it, it'd be much appreciated.

share|improve this question

closed as off topic by Walter, Yusubov, MainMa, GlenH7, Yannis Rizos Oct 22 '12 at 18:42

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Hello and Welcome to Programmers. Please don't cross post questions. You've already asked this question on Stack Overflow. Have a pleasant day :) –  World Engineer Oct 3 '12 at 1:47
    
@WorldEngineer yes I did, but someone on SO told me to post it here, so that's why I posted it here. I'm assuming it will be removed from SO –  user1647484 Oct 3 '12 at 1:50
1  
Questions should be migrated not cross posted. You should flag your question for moderator attention to facilitate this. –  ChrisF Oct 3 '12 at 8:13
    
question was closed on SO. –  user1647484 Oct 3 '12 at 22:10
    
@user1647484 You can still flag a closed question. –  deadly Oct 11 '12 at 15:36

1 Answer 1

up vote 4 down vote accepted

What you're looking for is a debugger. With ruby 1.8, the ruby-debug gem provides the canonical one, and in ruby 1.9, the debugger gem provides a version of its successor that is well-maintained.

Start your program with the gem loaded, for example ruby -rdebugger yourfile.rb, and you'll have access to the debugger.

The debugger kicks in at a breakpoint, which is a signal to suspend execution, and give you a chance to poke around. Typically, the easiest way to set a breakpoint is to add a source line at the point in execution you're interested in. Inserting a call to debugger, does this. Lets debug this function that asks for a user name, but restricts it to doctors. It's not letting "Dr. Bob" log in:

def get_user
  debugger  # This is where we break.
  name = ""
  begin
    print "What is your name?: "
    name = gets
  end until name.match /\ADr /
  puts "Welcome, #{name}."
  name
end

user = get_user

Lets launch it:

ruby -rdebugger login.rb

The program starts executing, then the debugger breaks us at line four. It shows us the next statement which is to be executed next. We can get a more complete listing of our surroundings by typing l.

(rdb:1) l
[-1, 8] in login.rb
   1  
   2  def get_user
   3    debugger
=> 4    name = ""
   5    begin
   6      print "What is your name?: "
   7      name = gets 
   8    end until name.match /\ADr /

You can examine the result of expressions by using the p command. p name will show us that the name variable is an empty string at this point of execution.

n steps, which executes the next line, and returns control to the debugger. s will allow you to step into a method call. c continues execution until the program terminates, or hits another breakpoint. In this case, we'd probably want to use the n command to trace the execution of this method. Just these handful of commands describes the vast majority of my debugging sessions.

The debugger has has interactive help which can be accessed with help. A more conversational tutorial on the web is: Debugging with ruby-debug

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.