# Ad-hoc debugging module

**URL:** <https://www.ruby-forum.com/t/ad-hoc-debugging-module/91342>\
**Category:** Ruby\
**Created:** [April 7, 2007, 2:54am UTC](https://www.ruby-forum.com/t/ad-hoc-debugging-module/91342 "2007-04-07T02:54:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbe5000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb9ed0/32.png) [@rbe5000](https://www.ruby-forum.com/u/rbe5000)\
**Post date:** [April 7, 2007, 2:54am UTC](https://www.ruby-forum.com/t/ad-hoc-debugging-module/91342/1 "2007-04-07T02:54:42Z")

</div>

Hi,

I recently found myself playing with a bunch of objects, iterating  
their behavior in irb, and I wanted to see just some of the fields  
each time I did something instead of the whole object that you would  
get with pp or y .

So, I wrote a little module that I could include in interesting  
classes, so that I could ask objects things like obj.debug  
(:field1, :field5, :field6) instead of printing out all the methods.  
Then I got bored doing that and extended it so I could create ad hoc  
watch methods, e.g., obj.show\_those3(:field1, :field5, :field6).

I thought others might find it useful too. Also, if anyone has any  
improvements on it, I am of course interested.

Thanks,  
Bob E.

> **[junitfactory.com](http://www.junitfactory.com/)**
>
> JUnit,Automated Software Test Tools,Plugin,Software Developer Companies

module Debuggable  
def method\_missing(method\_name, \*args)  
method\_name = method\_name.to\_s  
if method\_name.eql?(‘debug’)  
_debug(args)  
elsif method\_name =~ /^show_/  
new\_method\_name = create\_new\_watch\_method(method\_name, args)  
self.method(new\_method\_name).call  
elsif method\_name =~ /^unshow\_/  
self.class.class\_eval do  
watch\_name = method\_name.slice(method\_name.index(/unshow\_/)

- 7, method\_name.length)  
undef\_method “show\_#{watch\_name}”  
end  
end  
end

# =================== given a class

class MyClass  
attr\_reader :name, :date

def initialize(name, date)  
@name = name  
@date = date  
end  
end

# =================== loaded in an irb prompt, we can do the following:

%\>m = MyClass.new(“Bob”, Time.now)  
%\> class MyClass  
include Debuggable  
end

# %\>m.debug(:date)

@date = Fri Apr 06 17:15:25 -0700 2007

# %\> m.debug(:all)

@name = Bob, @date = Fri Apr 06 17:15:25 -0700 2007

# %\> m.debug

@name = Bob, @date = Fri Apr 06 17:15:25 -0700 2007

# %\>m.show\_date(:date) created permanent watch method: show\_date

@date = Fri Apr 06 17:15:25 -0700 2007

%\>m.methods.grep /show/  
[“show\_date”]

# %\>m.show\_date

@date = Fri Apr 06 17:15:25 -0700 2007

%\>m.unshow\_date  
%\>m.methods.grep /show/  
[]

# %\>m.show\_all created permanent watch method: show\_all

@name = Bob, @date = Fri Apr 06 17:15:25 -0700 2007

%\>m.methods.grep /show/  
[“show\_all”]

# %\>m.show\_all

@name = Bob, @date = Fri Apr 06 17:15:25 -0700 2007

%\>m.unshow\_all  
%\>m.methods.grep /show/  
[]

# %\>m.show\_bazooka created permanent watch method: show\_bazooka

@name = Bob, @date = Fri Apr 06 17:15:25 -0700 2007
