On Nov 28, 8:37 pm, MonkeeSage <MonkeeS... / gmail.com> wrote:
> 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[right-click save-as]
>
> Regards,
> Jordan
>
> [1]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