how to understand Ruby code? (noting various mixins/includes) Isthere a tool to assist here?

G

Greg Hauptmann

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. =A0There 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). =A0Just
trying to make sense of the final object structure and where methods
resides seems to be a challenge. =A0The latest one I've been looking at
are things like AuthLogic and Paperclip which are used in the Rails
world.

QUESTION 1: =A0Is there a tool that could assist here? =A0I'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 =A02: 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
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

On Mon, Dec 7, 2009 at 4:47 PM, Greg Hauptmann <
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.
 
F

Florian Gilcher

On Mon, Dec 7, 2009 at 4:47 PM, Greg Hauptmann <


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 Gilcher
 
D

David Masover

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.
 
G

Greg Hauptmann

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_r=
ecord/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/t=
emplates/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/t=
emplates/template.rb:225:in
`erb'
from /opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/../lib/yard/t=
emplates/template.rb:306:in
`render_section'
from /opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/../lib/yard/t=
emplates/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/t=
emplates/template.rb:191:in
`each'
from /opt/local/lib/ruby/gems/1.8/gems/yard-0.4.0/bin/../lib/yard/t=
emplates/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/c=
li/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 Gilcher said:
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 y= ou
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 defi= nes
multiple
helper classes and modules under his namespace.

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

Regards,
Florian Gilcher



--=20
Greg
http://blog.gregnet.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top