Snippet: debugging (and a question)

Discussion in 'Ruby' started by Moses Hall, Jul 11, 2003.

  1. Moses Hall

    Moses Hall Guest

    Greetings.

    I'm a recent Ruby convert and I absolutely love it. No other lingo looks
    so much (or for that matter, even vaguely) like pseudocode! Great stuff.

    Anyway, in my code I found myself keeping an array of the methods for
    which I wanted verbose debugging info, to get info printed only
    during the execution of particular method(s). Previously I would check
    in each method whether its name was in the array:

    def method1(x)
    debug = @@debug.member? "method1"
    printf "method1 called with %s\n", x.inspect if debug
    ...
    end

    Then I came up with a way to reduce this to a "debug" method, which gets the
    stack and grabs the (calling) method's name and then returns true if that
    is in the global array o' method names.

    It's kinda skanky, anyone have a better, safer, cleaner,
    and/or cooler way to do this?
    It will break if the format of the stack dump changes.
    I like doing this sort of thing because it thins out the source code,
    keeps focus on the algorithm not infrastructure.


    def debug
    method = caller[0]
    # A Perly way to get the caller from
    # the line "... in `method_name'"
    method =~ /in\s`(.+)'/i
    return @@debug.member? $1
    end

    Thanks,

    /\/\oses
    "".rot13
    www.blugs.com
    Moses Hall, Jul 11, 2003
    #1
    1. Advertising

  2. On Sat, Jul 12, 2003 at 07:58:16AM +0900, Moses Hall wrote:
    > Greetings.
    >
    > I'm a recent Ruby convert and I absolutely love it. No other lingo looks
    > so much (or for that matter, even vaguely) like pseudocode! Great stuff.
    >
    > Anyway, in my code I found myself keeping an array of the methods for
    > which I wanted verbose debugging info, to get info printed only
    > during the execution of particular method(s). Previously I would check
    > in each method whether its name was in the array:
    >
    > def method1(x)
    > debug = @@debug.member? "method1"
    > printf "method1 called with %s\n", x.inspect if debug
    > ...
    > end
    >
    > Then I came up with a way to reduce this to a "debug" method, which gets the
    > stack and grabs the (calling) method's name and then returns true if that
    > is in the global array o' method names.
    >
    > It's kinda skanky, anyone have a better, safer, cleaner,
    > and/or cooler way to do this?


    You can use aspects (AspectR) to add logging to the desired methods.

    --
    _ _
    | |__ __ _| |_ ___ _ __ ___ __ _ _ __
    | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \
    | |_) | (_| | |_\__ \ | | | | | (_| | | | |
    |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_|
    Running Debian GNU/Linux Sid (unstable)
    batsman dot geo at yahoo dot com

    * dpkg ponders: 'C++' should have been called 'D'
    -- #Debian
    Mauricio Fernández, Jul 12, 2003
    #2
    1. Advertising

  3. Moses Hall

    Moses Hall Guest

    Thank you. AspectR looks like it is definitely worth checking out.

    > You can use aspects (AspectR) to add logging to the desired methods.
    Moses Hall, Jul 13, 2003
    #3
    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. Hedr
    Replies:
    1
    Views:
    560
    Raymond Hettinger
    Jun 25, 2003
  2. Replies:
    14
    Views:
    590
    Christian
    Apr 15, 2007
  3. Chris Stankevitz
    Replies:
    33
    Views:
    1,435
    Chris Stankevitz
    Nov 19, 2011
  4. Replies:
    2
    Views:
    96
    Jürgen Exner
    Dec 1, 2007
  5. R.P.
    Replies:
    4
    Views:
    107
    Thomas 'PointedEars' Lahn
    Nov 17, 2008
Loading...

Share This Page