# Like python's dir(), except better

**URL:** <https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589>\
**Category:** Ruby\
**Created:** [November 29, 2007, 3:41am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589 "2007-11-29T03:41:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 29, 2007, 3:41am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/1 "2007-11-29T03:41:33Z")

</div>

People often want to look at an object’s attributes when debugging.  
[1] Python has a handy dir() function that lists the attributes of an  
object. And I use it tons. I often use obj.methods.sort in ruby for  
the same purpose. But that doesn’t show me class variables. There are  
a handful of reflection functions to show different types of metadata  
about an object.

So…this is my attempt to create a “Super dir() with extra-strength,  
industrial, ruby power”. I probably failed, and it’s probably buggy as  
I just hacked it together in about 15 minutes. In other words, I’ve  
not tested it very extensively, YMMV (in fact your hair may  
spontaneously catch on fire and you may start chanting backwards in  
Latin…don’t blame me; I’ve warned you!).

class DirObject  
def initialize(attrs=[])  
@attributes = attrs  
end  
def push(metadata)  
@attributes \<\< metadata  
end  
def include?(test)  
@attributes.each { | key, value |  
return true if value.include?(test)  
}  
false  
end  
def show  
puts @attributes.map { | key, value |  
#key + “:\n " + value.join(”\n “)  
key + “:\n " + value.inspect  
}.join(”\n\n”)  
self  
end  
end

class Object  
private  
def \_get\_metadata(meta)  
begin  
send(meta)  
rescue NameError  
[]  
end  
end  
def _format\_meta(meta)  
unless (idx = meta.index("_")).nil?  
meta[0] = meta[0].chr.upcase  
meta[idx+1] = meta[idx+1].chr.upcase  
meta = meta[0…idx-1] + " " + meta[idx+1…-1]  
else  
meta[0] = meta[0].chr.upcase  
meta  
end  
end  
def \_do\_metadata(meta, dirobj)  
data = \_get\_metadata(meta)  
unless data.empty?  
meta = \_format\_meta(meta)  
dirobj.push([meta, data.sort])  
end  
dirobj  
end  
public  
def dir(verbose=false)  
dirobj = DirObject.new  
\_do\_metadata(“class\_variables”, dirobj)  
\_do\_metadata(“instance\_variables”, dirobj)  
\_do\_metadata(“methods”, dirobj)  
\_do\_metadata(“instance\_methods”, dirobj)  
if verbose  
\_do\_metadata(“private\_methods”, dirobj)  
\_do\_metadata(“private\_instance\_methods”, dirobj)  
\_do\_metadata(“protected\_methods”, dirobj)  
\_do\_metadata(“protected\_instance\_methods”, dirobj)  
\_do\_metadata(“public\_methods”, dirobj)  
\_do\_metadata(“public\_instance\_methods”, dirobj)  
\_do\_metadata(“singleton\_methods”, dirobj)  
end  
dirobj  
end  
def ls(verbose=false)  
dir(verbose).show  
end  
end

Comments, corrections and criticisms welcome!

Ps. Object#dir actually prints the metadata (like python’s dir()), and  
#ls just gives you a DirObject which is really only useful to do an  
includes? on (which searches for a value through the entire metadata).

[http://monkeesage.prohosts.org/ruby/dir.rb](http://monkeesage.prohosts.org/ruby/dir.rb) [right-click save-as]

Regards,  
Jordan

[1]  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/d107b1318bbfdfd1/afa5a69d30007a64?lnk=gst&q=python+dir()#msg\_77d85f05e1ae8e66](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/d107b1318bbfdfd1/afa5a69d30007a64?lnk=gst&q=python+dir()#msg_77d85f05e1ae8e66)

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 29, 2007, 8:20am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/2 "2007-11-29T08:20:53Z")

</div>

Quoth Robert D.:

> of\_object.methods …
> 
> an\_object.dir # which would be distroyed by overloading _very often_  
> anyway becomes
> 
> DirObject.new( an\_object ).
> 
> Cheers  
> Robert

Well, it’s not like it’s _wrong_ to mess with object as long as you’re  
not  
overwriting stuff. Or so it seems to me.

Regards,

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 29, 2007, 8:08am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/3 "2007-11-29T08:08:02Z")

</div>

On Nov 29, 2007 3:40 AM, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
Great idea!  
I would however leave Object alone, and put all code into DirObject.  
Thus DirObject has to explore Object which might be a little bit  
harder and slower, but you definitely do not want to mess with Object,  
right?  
Just a short example of what I have in mind  
\_\_do\_metadata("methods"n, do) becomes

def DirMethods#add\_methods( of\_object )  
of\_object.methods …

an\_object.dir # which would be distroyed by overloading _very often_  
anyway becomes

DirObject.new( an\_object ).

## Cheers Robert

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

All truth passes through three stages. First, it is ridiculed. Second,  
it is violently opposed. Third, it is accepted as being self-evident.  
Schopenhauer (attr.)

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 29, 2007, 8:26am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/4 "2007-11-29T08:26:10Z")

</div>

On Nov 29, 2007 8:20 AM, Konrad M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def DirMethods#add\_methods( of\_object )  
> > Well, it’s not like it’s _wrong_ to mess with object as long as you’re not  
> > overwriting stuff. Or so it seems to me.

I am not sure about the principle but please do not forget that people  
will overwrite _your stuff_.

## Robert

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

All truth passes through three stages. First, it is ridiculed. Second,  
it is violently opposed. Third, it is accepted as being self-evident.  
Schopenhauer (attr.)

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 29, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/5 "2007-11-29T23:55:13Z")

</div>

On Nov 29, 7:30 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Kind regards
> 
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

Example please?

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 29, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/6 "2007-11-29T14:31:06Z")

</div>

2007/11/29, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> People often want to look at an object’s attributes when debugging.  
> [1] Python has a handy dir() function that lists the attributes of an  
> object. And I use it tons. I often use obj.methods.sort in ruby for  
> the same purpose.

You can view the attributes quite conveniently with pp (you’ll see  
instance variables nicely nested and indented as a tree).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 30, 2007, 12:06am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/7 "2007-11-30T00:06:21Z")

</div>

On Nov 29, 1:06 am, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> of\_object.methods …  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> * * *
> 
> All truth passes through three stages. First, it is ridiculed. Second,  
> it is violently opposed. Third, it is accepted as being self-evident.  
> Schopenhauer (attr.)

I don’t know? I’ve never heard that extending Object was bad? Seems  
like, if I want a method to operate on all instances of Object,  
putting the method in class Object is way to do it. I dislike the idea  
of initializing objects as a subclass to get #dir on them.

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 30, 2007, 12:11am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/8 "2007-11-30T00:11:37Z")

</div>

On Nov 29, 2007 4:45 PM, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > instance variables nicely nested and indented as a tree).  
> > Regards,  
> > Jordan

irb\> require ‘pp’  
=\> true  
irb\> class C; def initialize; @var = 1; end; end  
=\> nil  
irb\> pp( C.new )  
#\<C:0x2d8d3c4 @var=1\>  
=\> nil

Todd

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 30, 2007, 12:22am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/9 "2007-11-30T00:22:43Z")

</div>

On Nov 29, 5:10 pm, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > the same purpose.
> 
> #\<C:0x2d8d3c4 @var=1\>  
> =\> nil
> 
> Todd

Thanks. I’m aware of that, but it doesn’t show methods, class  
variables, and so on, and many times it just gives you a to\_s (e.g.,  
pp([1,2,3])). I was of the impression that Robert was talking about  
viewing _all_ of the object’s attributes with pp.

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 30, 2007, 12:07am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/10 "2007-11-30T00:07:31Z")

</div>

On Nov 28, 8:37 pm, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> not tested it very extensively, YMMV (in fact your hair may  
> def include?(test)  
> self  
> end  
> end  
> dirobj = DirObject.new  
> \_do\_metadata(“public\_instance\_methods”, dirobj)
> 
> Ps. Object#dir actually prints the metadata (like python’s dir()), and  
> #ls just gives you a DirObject which is really only useful to do an  
> includes? on (which searches for a value through the entire metadata).
> 
> [http://monkeesage.prohosts.org/ruby/dir.rb](http://monkeesage.prohosts.org/ruby/dir.rb)[right-click save-as]
> 
> Regards,  
> Jordan
> 
> [1][http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/d1](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/d1)…

Bug…#def should be returning nil

def ls(verbose=false)  
dir(verbose).show  
nil  
end

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![monkeesage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@monkeesage](https://www.ruby-forum.com/u/monkeesage)\
**Post date:** [November 30, 2007, 11:38am UTC](https://www.ruby-forum.com/t/like-pythons-dir-except-better/121589/11 "2007-11-30T11:38:15Z")

</div>

On Nov 30, 2007 12:05 AM, MonkeeSage [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > –
> > 
> > [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> > 
> > * * *
> > 
> > All truth passes through three stages. First, it is ridiculed. Second,  
> > it is violently opposed. Third, it is accepted as being self-evident.  
> > Schopenhauer (attr.)
> 
> I don’t know? I’ve never heard that extending Object was bad?  
> No it is not, I do it a lot, but it depends on your intentions.  
> If you are doing this for your own this is fine. However I believe  
> that such a feature shall rather play the rule  
> of a framework, well than there is really not much doubt IMHO,  
> extending Object is bad, your functionality will  
> just go away, especially as you are using such a common method name as  
> #dir.  
> like, if I want a method to operate on all instances of Object,  
> putting the method in class Object is way to do it. I dislike the idea  
> of initializing objects as a subclass to get #dir on them.  
> I do not interpret my code that way at all, a DirObject is an object  
> that has gathered information of a contained object, seems pretty much  
> straight forward to me??  
> And nobody will break it unless monkey patching it.

BTW do you not create a DirObject yourself in Object#dir?

Cheers  
Robert

–

[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

All truth passes through three stages. First, it is ridiculed. Second,  
it is violently opposed. Third, it is accepted as being self-evident.  
Schopenhauer (attr.)
