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

Discussion in 'Ruby' started by Greg Hauptmann, Dec 7, 2009.

  1. 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
    Greg Hauptmann, Dec 7, 2009
    #1
    1. Advertising

  2. Greg Hauptmann

    Tony Arcieri Guest

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

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

    --
    Tony Arcieri
    Medioh/Nagravision
    Tony Arcieri, Dec 8, 2009
    #2
    1. Advertising

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

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



    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
    Florian Gilcher, Dec 8, 2009
    #3
  4. On Monday 07 December 2009 05:47:11 pm Greg Hauptmann 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.
    David Masover, Dec 8, 2009
    #4
  5. 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 <>:
    >
    > On Dec 7, 2009, at 8:45 PM, Tony Arcieri wrote:
    >
    >> On Mon, Dec 7, 2009 at 4:47 PM, Greg Hauptmann <
    >> > wrote:
    >>
    >>> Just trying to make sense of the final object structure and where metho=

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

    >>
    >> I feel your pain, especially with AuthLogic. =A0It'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 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/
    Greg Hauptmann, Dec 8, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thinktwice
    Replies:
    10
    Views:
    1,759
    EventHelix.com
    Jul 1, 2008
  2. John Lam
    Replies:
    1
    Views:
    70
    James Britt
    Apr 27, 2005
  3. Gerald Bauer
    Replies:
    0
    Views:
    96
    Gerald Bauer
    Jul 9, 2008
  4. Greg Hauptmann
    Replies:
    5
    Views:
    174
    David A. Black
    Jan 20, 2009
  5. Sue
    Replies:
    7
    Views:
    148
    Richard Cornford
    Dec 10, 2003
Loading...

Share This Page