REG: How can we debug a ruby script

Discussion in 'Ruby' started by ext-golla.anil-kumar@nokia.com, Mar 30, 2009.

  1. Guest

    Hi,
    I want to debug a ruby script using -rdebug option.
    The following is the source code: with file fact.rb.
    Fact.rb
    def fact(n)
    return 1 if n =3D=3D 0
    f =3D 1
    n.downto(1) do |i|
    f *=3D i
    end
    return f
    end
    print fact(2), "\n"

    Debugging
    When I run the following command and type 'n' for debugin the next line, it=
    is going in to some other files.
    As shown below.
    C:\ruby\samples\RubySrc-1.8.6-p111\sample>ruby -rdebug fact.rb
    Debug.rb
    Emacs support available.

    c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    (rdb:1) list
    [5, 14] in c:/ruby/lib/ruby/site_ruby/1.8/ubygems.rb
    5 # All rights reserved.
    6 # See LICENSE.txt for permissions.
    7 #++
    8
    9
    =3D> 10 require 'rubygems'
    (rdb:1) n
    c:/ruby/lib/ruby/site_ruby/1.8/rubygems.rb:10:require 'rbconfig'
    (rdb:1) n
    c:/ruby/lib/ruby/1.8/i386-mswin32/rbconfig.rb:5:module Config
    (rdb:1) n
    c:/ruby/lib/ruby/1.8/i386-mswin32/rbconfig.rb:153:RbConfig =3D Config # com=
    patibility for ruby-1.9
    (rdb:1) n
    c:/ruby/lib/ruby/1.8/i386-mswin32/rbconfig.rb:154:CROSS_COMPILING =3D nil u=
    nless defined? CROSS_COMPILING
    (rdb:1) n
    c:/ruby/lib/ruby/1.8/i386-mswin32/rbconfig.rb:154:CROSS_COMPILING =3D nil u=
    nless defined? CROSS_COMPILING
    (rdb:1) n
    c:/ruby/lib/ruby/site_ruby/1.8/rubygems.rb:12:module Gem
    (rdb:1) n
    c:/ruby/lib/ruby/site_ruby/1.8/rubygems.rb:18:module Kernel
    (rdb:1)'

    What is the command that iam supposed to used to debug actual source code, =
    line by line( with out moving to other files)?

    Thanks and Regards,
    Anil kumar
     
    , Mar 30, 2009
    #1
    1. Advertising

  2. Something in your environment is making ruby do -rubygems (require
    'rubygems')
    Perhaps the RUBYOPT environment variable is set, because I can replicate
    your problem like this:

    $ export RUBYOPT=-rubygems
    $ ruby -rdebug fact.rb
    Debug.rb
    Emacs support available.

    /usr/local/lib/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    (rdb:1)

    So if this is the case, solution 1 is simply to unset the RUBYOPT
    variable.

    Solution 2 is to set a breakpoint at the start of your program.

    $ ruby -rdebug fact.rb
    Debug.rb
    Emacs support available.

    /usr/local/lib/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    (rdb:1) b fact.rb:1
    Set breakpoint 1 at fact.rb:1
    (rdb:1) cont
    Breakpoint 1, toplevel at fact.rb:1
    fact.rb:1:def fact(n)
    (rdb:1) n
    fact.rb:9:print fact(2), "\n"
    (rdb:1) n
    2
    $

    Solution 3 is to require rubygems *before* requiring debug, because when
    it has been required once, it won't be required again:

    $ ruby -rubygems -rdebug fact.rb
    Debug.rb
    Emacs support available.

    fact.rb:1:def fact(n)
    (rdb:1)

    This third one probably involves the least work.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Mar 31, 2009
    #2
    1. Advertising

  3. Guest

    Re: REG: How can we debug a ruby script in unit test classes

    Hi,
    Using the below method mention the below mail, I was successfully able to r=
    un and debug the script.
    But when I run unittest class, the flow goes into testcase case class as be=
    low.

    Testscript=20
    require "test/unit"
    =20
    class TestSimpleNumber < Test::Unit::TestCase
    =20
    def test_simple
    assert_equal("abc", "abc" )
    =20
    end
    =20
    def test_typecheck
    assert_equal("abc", "abc" )
    end
    =20
    def test_failure
    assert_equal("abc", "abc" )
    end
    =20
    End



    Execution:


    C:\ruby>ruby -rubygems -rdebug unittest.rb
    Debug.rb
    Emacs support available.

    unittest.rb:1:require "test/unit"
    (rdb:1) b 6
    Set breakpoint 1 at unittest.rb:6
    (rdb:1) cont
    Loaded suite unittest
    Started
    Breakpoint 1, test_simple at unittest.rb:6
    unittest.rb:6: assert_equal("abc", "abc" )
    (rdb:1) n
    c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:85: begin
    (rdb:1) n
    c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:86: teardown
    (rdb:1) n
    c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:94: result.add_run
    (rdb:1) n
    c:/ruby/lib/ruby/1.8/test/unit/testcase.rb:95: yield(FINISHED, name)
    (rdb:1) n
    c:/ruby/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:47: noti=
    fy_listeners(channel, value)
    (rdb:1)


    Please help me in this regard.

    Thanks,
    Anil kumar.


    >-----Original Message-----
    >From: [mailto:]=20
    >Sent: 31 March, 2009 12:14
    >To: ruby-talk ML
    >Subject: Re: REG: How can we debug a ruby script
    >
    >Something in your environment is making ruby do -rubygems (require
    >'rubygems')
    >Perhaps the RUBYOPT environment variable is set, because I can=20
    >replicate your problem like this:
    >
    >$ export RUBYOPT=3D-rubygems
    >$ ruby -rdebug fact.rb
    >Debug.rb
    >Emacs support available.
    >
    >/usr/local/lib/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    >(rdb:1)
    >
    >So if this is the case, solution 1 is simply to unset the=20
    >RUBYOPT variable.
    >
    >Solution 2 is to set a breakpoint at the start of your program.
    >
    >$ ruby -rdebug fact.rb
    >Debug.rb
    >Emacs support available.
    >
    >/usr/local/lib/site_ruby/1.8/ubygems.rb:10:require 'rubygems'
    >(rdb:1) b fact.rb:1
    >Set breakpoint 1 at fact.rb:1
    >(rdb:1) cont
    >Breakpoint 1, toplevel at fact.rb:1
    >fact.rb:1:def fact(n)
    >(rdb:1) n
    >fact.rb:9:print fact(2), "\n"
    >(rdb:1) n
    >2
    >$
    >
    >Solution 3 is to require rubygems *before* requiring debug,=20
    >because when it has been required once, it won't be required again:
    >
    >$ ruby -rubygems -rdebug fact.rb
    >Debug.rb
    >Emacs support available.
    >
    >fact.rb:1:def fact(n)
    >(rdb:1)
    >
    >This third one probably involves the least work.
    >--
    >Posted via http://www.ruby-forum.com/.
    >
    >=
     
    , Apr 19, 2009
    #3
  4. Phlip Guest

    Re: REG: How can we debug a ruby script in unit test classes

    Tips for posting:

    Don't reply to someone else's thread and change the name. Start a new thread
    with your New button.

    Now to your problem - is this a typo?

    > End


    The code should not compile with that error, but your stack trace implies
    you started executing. However...

    > C:\ruby>ruby -rubygems -rdebug unittest.rb


    You might need -rrubygems.

    Next, report what happens without the -rdebug. I never debug unit tests -
    they tend to help you avoid excess debugging!

    --
    Phlip
     
    Phlip, Apr 19, 2009
    #4
  5. Phlip Guest

    Re: REG: How can we debug a ruby script in unit test classes

    Phlip wrote:
    > Tips for posting:
    >
    > Don't reply to someone else's thread and change the name. Start a new thread
    > with your New button.


    My bad! I didn't notice the thread was the same.

    Carry on! (;
     
    Phlip, Apr 19, 2009
    #5
  6. Re: REG: How can we debug a ruby script in unit test classes

    Phlip wrote:

    >> C:\ruby>ruby -rubygems -rdebug unittest.rb

    >
    > You might need -rrubygems.


    -rubygems is correct (the file "ubygems.rb" exists for this purpose)

    > Next, report what happens without the -rdebug. I never debug unit tests


    The problem may be that Test::Unit does strange stuff with atexit to
    start the tests. I'm afraid I don't have a good solution for using
    -rdebug with Test::Unit. Try writing a separate standalone program which
    replicates the problem.

    Regards,

    Brian.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Apr 19, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RonL
    Replies:
    0
    Views:
    769
  2. ringos75
    Replies:
    0
    Views:
    988
    ringos75
    Apr 14, 2005
  3. Mike C. Fletcher
    Replies:
    3
    Views:
    1,005
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Oct 12, 2003
  4. André
    Replies:
    3
    Views:
    1,647
  5. 3lionz Wexler
    Replies:
    2
    Views:
    741
    Suraj Kurapati
    Aug 24, 2010
Loading...

Share This Page