Equivalent of python's __debug__

python can compile in two modes, normal and optimized. in optimized
mode, the read-only builtin variable debug will be set to 0, and
thus code fragments like this:

if debug:
print “there are %d eggs in the basket now.” % (eggs)

will be optimized away.

is there similar thing in ruby? or do i have to use a preprocessor?

···


dave

Hi.

if debug:
print “there are %d eggs in the basket now.” % (eggs)
will be optimized away.
is there similar thing in ruby? or do i have to use a preprocessor?

Interpreter switch ‘-d’ and $DEBUG may help you. This may be good for
tiny script, but running large application with -d switch is often so
verbose. I recommands you to use some logger library.

Example (using OBAQ):
require ‘obaq/log’

class MyClass
LOG = Obaq::Log.class_log(self)

def func
LOG.debug {
# This block only performs when logger is configured debugging
mode.
"…"
}
end
end

Thanks.

···


shirai@p1jp.com

Shirai,Kaoru
PlusOne Co.,Ltd

Shirai,Kaoru wrote:

if debug:
print “there are %d eggs in the basket now.” % (eggs)
will be optimized away.
is there similar thing in ruby? or do i have to use a preprocessor?

Interpreter switch ‘-d’ and $DEBUG may help you. This may be good for
tiny script, but running large application with -d switch is often so
verbose. I recommands you to use some logger library.

sorry for the late response. my problem is, i want to insert lots of
string literals (sort of embedded documentation, but will be read by
other scripts) which i don’t want to include in the optimized version.
in python, if i do this:

def foo(self, x, y):
if debug:
self.__doc__x = "…some longish string…"
self.__doc__y = "…some also longish string…"
self.doc_spec1 = “…yet another string…”

then the debug portion along with the literals will be removed so they
don’t take up memory. i don’t seem to be able to do this with ruby,
except by using a preprocessor.

···


dave

David Garamond wrote:

Shirai,Kaoru wrote:

if debug:
print “there are %d eggs in the basket now.” % (eggs)
will be optimized away.
is there similar thing in ruby? or do i have to use a preprocessor?

Interpreter switch ‘-d’ and $DEBUG may help you. This may be good for
tiny script, but running large application with -d switch is often so
verbose. I recommands you to use some logger library.

sorry for the late response. my problem is, i want to insert lots of
string literals (sort of embedded documentation, but will be read by
other scripts) which i don’t want to include in the optimized version.
in python, if i do this:

def foo(self, x, y):
if debug:
self.__doc__x = "…some longish string…"
self.__doc__y = "…some also longish string…"
self.doc_spec1 = “…yet another string…”

then the debug portion along with the literals will be removed so they
don’t take up memory. i don’t seem to be able to do this with ruby,
except by using a preprocessor.

I’m a newbie and I still haven’t read any serious book, just been
browsing code --I’ve code prefixed with a D that appeared to be debug
code, so maybe you’d do

D self.__doc__x …
D self.__doc__y …

and then ruby -d yourscript.rb

···


Giuseppe “Oblomov” Bilotta

Axiom I of the Giuseppe Bilotta
theory of IT:
Anything is better than MS

def foo(self, x, y):
     if __debug__:
         self.__doc__x = "...some longish string..."
         self.__doc__y = "...some also longish string..."
         self.__doc_spec1__ = "...yet another string..."
         ...
     ...

Well, I've perhaps not understood but you can write (like previously said)

pigeon% ruby -e 'if $DEBUG; a = "a long string"; p a; end'
pigeon%

pigeon% ruby -de 'if $DEBUG; a = "a long string"; p a; end'
"a long string"
pigeon%

Guy Decoux

ts wrote:

“D” == David Garamond davegaramond@icqmail.com writes:

def foo(self, x, y):
if debug:
self.__doc__x = "…some longish string…"
self.__doc__y = "…some also longish string…"
self.doc_spec1 = “…yet another string…”

Well, I’ve perhaps not understood but you can write (like previously said)

pigeon% ruby -e 'if $DEBUG; a = “a long string”; p a; end’
pigeon%

pigeon% ruby -de ‘if $DEBUG; a = “a long string”; p a; end’
"a long string"
pigeon%

but “a long string” still gets allocated by ruby. i want optimize away
the debug portion, so ruby doesn’t consider it exist at all.

···


dave

From: David Garamond [mailto:davegaramond@icqmail.com]
Sent: Saturday, August 24, 2002 8:47 AM
To: ruby-talk ML
Subject: Re: equivalent of python’s debug

ts wrote:

“D” == David Garamond davegaramond@icqmail.com writes:

def foo(self, x, y):
if debug:
self.__doc__x = "…some longish string…"
self.__doc__y = "…some also longish string…"
self.doc_spec1 = “…yet another string…”

Well, I’ve perhaps not understood but you can write (like
previously
said)

pigeon% ruby -e 'if $DEBUG; a = “a long string”; p a; end’
pigeon%

pigeon% ruby -de ‘if $DEBUG; a = “a long string”; p a; end’
"a long string"
pigeon%

but “a long string” still gets allocated by ruby. i want optimize
away
the debug portion, so ruby doesn’t consider it exist at all.

Well, Ruby does parse and place those (long string) elements within the
Abstract Syntax Tree of the interpreter, but they are not allocated
during every call through a method, so the impact is lower. Remember
that Ruby is interpreted so this minimization makes a difference.
Another idiom I often use is:

def foo
$DEBUG && (puts “entered foo”)
…other stuff…
end

Because it’s a logical && if $DEBUG is negative the right hand statement
will not be evaluated.

···

-----Original Message-----

You could place all your debug strings in a separate file and do this:
require ‘dbg-strings.rb’ if $DEBUG

  • alan
···

On Sat, Aug 24, 2002 at 09:47:14PM +0900, David Garamond wrote:

ts wrote:

“D” == David Garamond davegaramond@icqmail.com writes:

def foo(self, x, y):
if debug:
self.__doc__x = "…some longish string…"
self.__doc__y = "…some also longish string…"
self.doc_spec1 = “…yet another string…”

Well, I’ve perhaps not understood but you can write (like previously
said)
pigeon% ruby -e 'if $DEBUG; a = “a long string”; p a; end’
pigeon%

pigeon% ruby -de ‘if $DEBUG; a = “a long string”; p a; end’
"a long string"
pigeon%

but “a long string” still gets allocated by ruby. i want optimize away
the debug portion, so ruby doesn’t consider it exist at all.


Alan Chen
Digikata LLC