# Featurizing debug()

**URL:** <https://www.ruby-forum.com/t/featurizing-debug/76140>\
**Category:** Rails\
**Created:** [November 16, 2006, 7:34am UTC](https://www.ruby-forum.com/t/featurizing-debug/76140 "2006-11-16T07:34:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 16, 2006, 7:34am UTC](https://www.ruby-forum.com/t/featurizing-debug/76140/1 "2006-11-16T07:34:49Z")

</div>

Raility Checkers:

Everyone needs a little debug() now and then. The RHTML statement \<%=  
debug(x) %\> will barf out the value of x into the output page, inside  
a tasteful grey DIV.

Suppose x were a complex expression. Or suppose (quell horror!) we  
have several debugs, and they come and go.

Suppose I wanted debug() to also report the expression. debug(x + 1)  
would emit x + 1: 43.

How to do that in Ruby?

(I could do it trivially in \>cough\< C++…)

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 16, 2006, 8:00am UTC](https://www.ruby-forum.com/t/featurizing-debug/76140/2 "2006-11-16T08:00:51Z")

</div>

Well, you do it pretty much the way you would in \>cough\< C++:

irb(main):001:0\> def debug(exp)  
irb(main):002:1\> puts “#{exp} : #{eval(exp)}”  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> debug(‘3+2’)  
3+2 : 5  
=\> nil  
irb(main):005:0\>

Rember the C and C++ preprocessors only deal with strings, so when you  
use  
#define you’re just doing some string substitution wherever the macro  
appears in your \>cough\< code. The actual expressions are not evaluated  
until  
later (ok, ok, some preprocessors are smart enough to fold simple  
invariant  
expressions).

Does the above implementation of debug work for you?

Phlip wrote:

> >

–  
View this message in context:  
[http://www.nabble.com/-Rails--featurizing-debug()-tf2641121.html#a7372543](http://www.nabble.com/-Rails--featurizing-debug%28%29-tf2641121.html#a7372543)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 16, 2006, 1:13pm UTC](https://www.ruby-forum.com/t/featurizing-debug/76140/3 "2006-11-16T13:13:37Z")

</div>

You need to pass in a binding to introduce context into the eval.

A small bit of info can be found here:

[module Kernel - RDoc Documentation](http://www.ruby-doc.org/core/classes/Kernel.html#M005735)

V/r  
Anthony

On 11/16/06, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–  
Email: removed\_email\_address@domain.invalid  
Cell: 808 782-5046  
Current Location: Melbourne, FL

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 16, 2006, 8:30am UTC](https://www.ruby-forum.com/t/featurizing-debug/76140/4 "2006-11-16T08:30:53Z")

</div>

s.ross wrote:

> irb(main):001:0\> def debug(exp)  
> irb(main):002:1\> puts “#{exp} : #{eval(exp)}”  
> irb(main):003:1\> end

> irb(main):004:0\> debug(‘3+2’)  
> 3+2 : 5

> Does the above implementation of debug work for you?

Wouldn’t the eval() miss the scope of the calling function?

(If you can keep a secret, I still haven’t figure out how to pass  
scope, continuances (?), and locals into eval()…)

–  
Phlip
