M
Moses Hall
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
"(e-mail address removed)".rot13
www.blugs.com
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
"(e-mail address removed)".rot13
www.blugs.com