Ruby: September 2007 Archives

ParseTree is a C extension (using RubyInline) that extracts the parse tree for an entire class or a specific method and returns it as a s-expression (aka sexp) using ruby's arrays, strings, symbols, and integers.

As an example:

def conditional1(arg1) if arg1 == 0 then return 1 end return 0 end

becomes:

[:defn, :conditional1, [:scope, [:block, [:args, :arg1], [:if, [:call, [:lvar, :arg1], :==, [:array, [:lit, 0]]], [:return, [:lit, 1]], nil], [:return, [:lit, 0]]]]]

  • Uses RubyInline, so it just drops in.
  • Includes SexpProcessor and CompositeSexpProcessor.
    • Allows you to write very clean filters.
  • Includes UnifiedRuby, allowing you to automatically rewrite ruby quirks.
  • ParseTree#parsetreefor_string lets you parse arbitrary strings of ruby.
  • Includes parsetreeshow, which lets you quickly snoop code.
    • echo "1+1" | parsetreeshow -f for quick snippet output.
  • Includes parsetreeabc, which lets you get abc metrics on code.
    • abc metrics = numbers of assignments, branches, and calls.
    • whitespace independent metric for method complexity.
  • Includes parsetreedeps, which shows you basic class level dependencies.
  • Does not work on the core classes, as they are not ruby (yet).

Changes:

  • 2 minor enhancements:

    • Deactivated gcc-specific compiler flags unless ENV['ANAL'] or on my domain.
    • Minor code cleanup - happier with -pedantic and the like.
  • 1 bug fix:

    • FINALLY conquered the splat args bug on certain platforms/versions. Special Thanks to Jonas Pfenniger for debugging this and providing a patch.
  • http://www.zenspider.com/ZSS/Products/ParseTree/

readability vs. speed

| | Comments (2)

Which one is better? You decide...

if true then # 0.75 seconds
  require 'active_record'
  require 'yaml'
  require 'app/models/author'
  require 'app/models/project'
  config = YAML.load(File.read('config/database.yml'))
  ActiveRecord::Base.establish_connection(config["development"])
else # 2.0 seconds
  require File.dirname(__FILE__) + '/../config/environment'
end

conn = ActiveRecord::Base.connection

I'm slightly partial to the faster running one (esp. during development)... but it did take me a while to figger it out. Loading config/environment a heavy price to pay just to get a DB connection and automatic requires.

About this Archive

This page is a archive of entries in the Ruby category from September 2007.

Ruby: August 2007 is the previous archive.

Ruby: October 2007 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.1