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
"zbfrf@oyhtf.pbz".rot13
www.blugs.com
You can use aspects (AspectR) to add logging to the desired methods.
···
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?
–
_ _
__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_
_ \ / ` | ’ \
) | (| | |__ \ | | | | | (| | | | |
.__/ _,|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com
- dpkg ponders: ‘C++’ should have been called ‘D’
– #Debian
Thank you. AspectR looks like it is definitely worth checking out.
···
You can use aspects (AspectR) to add logging to the desired methods.