How to understand Ruby code? (noting various mixins/includes) Is there a tool to assist here?

Hi,

I seem to spend a good deal of time trying to make sense of open
source ruby code due to the way it is very dynamic. There are various
code files that are really just ready to be “mixed in” to other files
(e.g. with the class methods & instance methods ready to go). Just
trying to make sense of the final object structure and where methods
resides seems to be a challenge. The latest one I’ve been looking at
are things like AuthLogic and Paperclip which are used in the Rails
world.

QUESTION 1: Is there a tool that could assist here? I’m thinking of
one that could do things like walk the code base and then:

  • Give a hierarchy structure where various files methods are loaded
    into which objects

  • Perhaps something that analyses code and then added comments to
    the various files highlighting where that files methods will reside in
    terms of the final object structure.

  • Given sample walk throughs of methods hit in which files for a
    given use case (e.g. like how the java IDEs can auto-sequence
    diagrams)

QUESTION 2: Should of nothing be available for the above, and tips/
tricks people have re understanding a new ruby codebase? (although in
particular with a rails bent)

thanks

On Mon, Dec 7, 2009 at 4:47 PM, Greg H. <
[email protected]> wrote:

Just trying to make sense of the final object structure and where methods
resides seems to be a challenge. The latest one I’ve been looking at
are things like AuthLogic and Paperclip which are used in the Rails
world.

I feel your pain, especially with AuthLogic. It’s very difficult to
follow
how methods are dispatched among a quagmire of mixins.

On Dec 7, 2009, at 8:45 PM, Tony A. wrote:

I feel your pain, especially with AuthLogic. It’s very difficult to
follow
how methods are dispatched among a quagmire of mixins.

I found that YARD does a good job at finding static relationships
between classes
as well as informing you about them. It also provides a tool that
draws you a graph
out of them (yard-graph).

For a sample: http://yardoc.org/docs/datamapper-dm-core/DataMapper/
Model (includes
2 modules, extends another one, is defined in more then one file and
defines multiple
helper classes and modules under his namespace.

It’s not 100%, but catches most reasonable cases. For more gathering,
there is still
Kernel#set_trace_fun. I experimented with a visualization for that for
university,
but it explodes horribly quick.

Regards,
Florian G.

On Monday 07 December 2009 05:47:11 pm Greg H. wrote:

QUESTION 1: Is there a tool that could assist here?

Probably. I know people have written tools which generate Graphviz
charts of
whole programs. I haven’t used these tools, though, especially when
there’s
decent documentation – so listen to the other posters on this one.

  • Given sample walk throughs of methods hit in which files for a
    given use case (e.g. like how the java IDEs can auto-sequence
    diagrams)

I don’t really know what this looks like. Ruby does have a debugger, but
as
you’ve used a Java IDE before, I assume the concept of a debugger isn’t
new to
you.

QUESTION 2: Should of nothing be available for the above, and tips/
tricks people have re understanding a new ruby codebase? (although in
particular with a rails bent)

Well, there’s always playing around in irb – or script/console, in
Rails.
There’s inserting puts and p statements into the code – with a local
branch,
obviously, so you can revert those.

The best tool, though, is good documentation, and especially, read the
specs,
or the tests. Rails seems to be trying to strongly encourage TDD, which
means
that every intended behavior should have a descriptively-named test for
it.
This will at least give you an idea of how the code is intended to be
used,
and that’s the very first thing you’ll need before trying to understand
how it
works.

thanks for the pointer to YARD - interestingly I just run across my
whole rails app (which has AuthLogic as a plugin), but it did give
some WARNINGS and finally an exception - I’ll have a better look

e.g.
[warn]: in YARD::Handlers::Ruby::Legacy::ClassHandler: Undocumentable
class ‘base’
[warn]: in file
‘./vendor/plugins/redhillonrails_core/lib/red_hill_consulting/core/active_record/schema.rb’:9:

    9:  class << base
    10:            attr_accessor :defining
    11:            alias :defining? :defining
    12:
    13:            alias_method_chain :define, :redhillonrails_core
    14:          end

/opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/templates/default/module/html/defines.erb:2:in
erb': wrong argument type Symbol (expected Proc) (TypeError) from /opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/../lib/yard/templates/template.rb:225:in erb’
from
/opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/templates/default/module/html/box_info.erb:28:in
erb' from /opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/../lib/yard/templates/template.rb:225:in erb’
from
/opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/…/lib/yard/templates/template.rb:306:in
render_section' from /opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/../lib/yard/templates/template.rb:196:in run’
from /opt/local/lib/ruby/1.8/erb.rb:743:in each_with_index' from /opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/../lib/yard/templates/template.rb:191:in each’
from
/opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/…/lib/yard/templates/template.rb:191:in
each_with_index' ... 38 levels... from /opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/../lib/yard/cli/yardoc.rb:29:in run’
from /opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/yardoc:4
from /opt/local/bin/yardoc:19:

2009/12/8 Florian G. [email protected]: