S
Simon Strandgaard
To ease unittesting I have made a #debug keyword, which enables
the global $debug flag for the specified method.
For instance
debug :test
is roughly equivalent with
def test
$debug = true
super
$debug = false
end
The "debug" keyword works inside classes. But not within modules.
Question: How can I make "debug" work within modules ?
--
Simon Strandgaard
server> ruby a.rb
before #test
42
after #test
server> expand -t4 a.rb
class Object
# enable the global debug flag for just a single method
def self.debug(*args)
args.each{|symbol|
begin
meth = instance_method(symbol)
rescue => e
$stderr.puts "ERROR: symbol2method failure, " + e.inspect
next
end
name = symbol.id2name
org = "_debug_"+name
arguments = (meth.arity != 0) ? "(*a,&b)" : "(&b)"
module_eval <<MSG
alias #{org} #{name}
def #{name}#{arguments}
$stdout.puts("before ##{name}")
$debug = true
#{org}#{arguments}
$stdout.puts("after ##{name}")
ensure
$debug = nil
end
private :#{org}
MSG
}
end
end
class Test
module A
def test
p 42
end
#debug :test # Why does'nt this work?
end
include A
debug :test
end
Test.new.test
server>
the global $debug flag for the specified method.
For instance
debug :test
is roughly equivalent with
def test
$debug = true
super
$debug = false
end
The "debug" keyword works inside classes. But not within modules.
Question: How can I make "debug" work within modules ?
--
Simon Strandgaard
server> ruby a.rb
before #test
42
after #test
server> expand -t4 a.rb
class Object
# enable the global debug flag for just a single method
def self.debug(*args)
args.each{|symbol|
begin
meth = instance_method(symbol)
rescue => e
$stderr.puts "ERROR: symbol2method failure, " + e.inspect
next
end
name = symbol.id2name
org = "_debug_"+name
arguments = (meth.arity != 0) ? "(*a,&b)" : "(&b)"
module_eval <<MSG
alias #{org} #{name}
def #{name}#{arguments}
$stdout.puts("before ##{name}")
$debug = true
#{org}#{arguments}
$stdout.puts("after ##{name}")
ensure
$debug = nil
end
private :#{org}
MSG
}
end
end
class Test
module A
def test
p 42
end
#debug :test # Why does'nt this work?
end
include A
debug :test
end
Test.new.test
server>