How to Debug Ruby

Ruby is a relatively easy programming language for Web development, and can be deployed quickly. Ruby programs facilitate dynamic Web pages that users can interact with. However, even though developers consider it a scripting language, a Ruby program of any complexity still needs debugging support. Debug Ruby programs easily by compiling scripts with the debugging flag and using the debugging global variable contained in the Ruby Interpreter.

Things You'll Need

  • Ruby Interpreter
Show More

Instructions

    • 1

      Create a Ruby file, "debug.rb," and copy the following code into it. The "upto" loop executes five times, and always prints due to the condition of the "if" statement reading "True":

      #!/usr/local/bin/ruby

      a = ARGV[0].to_i

      a.upto(5) do |x|
      if True then
      puts x
      end
      end

    • 2

      Change the "True" in the "if" statement to the debugging variable. This variable contains a value that the "if" statement reads. If the debug value reads "true," then the if statement will execute. This allows you to enter debugging commands into the program by running the program with the "-d" argument, such as "ruby -d debug.rb" at the command line:

      #!/usr/local/bin/ruby

      a = ARGV[0].to_i
      a.upto(5) do |x|
      if $DEBUG then
      puts x
      end
      end

    • 3

      Debug the entire program using the debugger. The Ruby debugger exists as a separate library that you can call from the Ruby Interpreter, with the intended file to debug as its argument. For example, the following command calls the ruby debug library with the "debug.rb" file as an argument, meaning it will debug errors in that file:

      $>ruby -rdebug debug.rb

Related Searches:

References

Comments

Related Ads

Featured