I wrote an inliner

Eric Hodel | Wed, 27 Jun 2007 07:35:00 GMT

$ ruby test.rb 
caller result: 12
caller:
def caller
  v1 = (2 + 3)
  x = callee(v1)
  (x + 2)
end
callee:
def callee(v)
  (v + 5)
end
 
inline callee into caller
caller result: 12
caller:
def caller
  v1 = (2 + 3)
  x = (inline_callee_v = v1
  (inline_callee_v + 5))
  (x + 2)
end
$ ruby -Ilib bm.rb 
Rehearsal -------------------------------------------
empty     0.090000   0.000000   0.090000 (  0.083842)
plain     1.030000   0.010000   1.040000 (  1.037302)
inlined   0.810000   0.000000   0.810000 (  0.821394)
---------------------------------- total: 1.940000sec
 
              user     system      total        real
empty     0.080000   0.000000   0.080000 (  0.084179)
plain     1.100000   0.000000   1.100000 (  1.105742)
inlined   0.900000   0.000000   0.900000 (  0.900799)

Posted in , ,  | no comments

render_tree for Rails

Eric Hodel | Fri, 08 Sep 2006 18:02:00 GMT

Spelunking deep in unfamiliar Rails view code? Flushing out the cobwebs and updating your code? Don't know what gets rendered when?

I have a solution for you:

class ActionView::Base

  alias plain_render render

  RENDERS = [:partial, :template, :file, :action, :text, :inline, :nothing,
             :update]

  def render(*args)
    @level ||= 0

    print '  ' * @level

    case args.first
    when String then
      p args.first
    when Hash then
      hash = args.first
      found = hash.keys & RENDERS
      if found.length == 1 then
        puts "%p => %p" % [found.first, hash[found.first]]
      else
        raise "Dunno: %p" % [hash]
      end
    else
      raise "Dunno: %p" % [args]
    end

    @level += 1
    result = plain_render(*args)
    @level -= 1
    result
  end

end

Drop that in test/render_tree.rb and require it in your tests when you want to see a tree like this:

$ ruby test/views/things_view_test.rb -n test_view
Loaded suite test/views/things_view_test
Started
"things/things-header"
  "things/sidebar"
    "widgets/forms/goal_form"
    :partial => "widgets/sidenav_boxes/invite_and_edit"
      "widgets/sidenav_boxes/_invite_and_edit"
    :partial => "widgets/forms/edit_worth_doing_form"
      "widgets/forms/_edit_worth_doing_form"
    :partial => "widgets/sidenav_boxes/tags"
      "widgets/sidenav_boxes/_tags"
    :partial => "widgets/sidenav_boxes/popular_places"
      "widgets/sidenav_boxes/_popular_places"
    :partial => "widgets/sidenav_boxes/google_ads"
      "widgets/sidenav_boxes/_google_ads"
    :partial => "widgets/sidenav_boxes/find_help"
      "widgets/sidenav_boxes/_find_help"
    :partial => "widgets/sidenav_boxes/people_who_reached_this_goal"
      "widgets/sidenav_boxes/_people_who_reached_this_goal"
    :partial => "widgets/sidenav_boxes/quotation"
      "widgets/sidenav_boxes/_quotation"
    :partial => "widgets/sidenav_boxes/goal_created_by"
      "widgets/sidenav_boxes/_goal_created_by"
"widgets/general/post_add_messages"
"things/shared_body"
  :partial => "widgets/goals_gallery_teaser"
    "widgets/_goals_gallery_teaser"
  :partial => "entries_bucket"
    "things/_entries_bucket"
  "widgets/forms/related_goals"
.
Finished in 1.205494 seconds.

1 tests, 7 assertions, 0 failures, 0 errors

(I think I'll end up throwing this into ZenTest.)

Posted in , ,  | 4 comments

Markov Chain

Eric Hodel | Sun, 26 Feb 2006 00:39:00 GMT

The back of my brain has been wanting a Markov chain based random-text generator for some time. I found some decently explanatory pseudo-code in the Generating Text section of the online Programming Pearls by Jon Bently.

What I really wanted was something that illustrated the concept well so I could tell what it was doing. The perl, python and ruby versions I found weren’t simple enough for me to see that. I also wanted to adjust the amount of state in the chain, but the only implementations that supported that were far too complicated to figure out.

Working from Jon Bently’s pseudo-code I ended up with the following implementation. Read more...

Posted in ,

httpdump

Eric Hodel | Mon, 23 Jan 2006 07:34:00 GMT

I wrote a nifty little combination of a WEBrick servlet and a ruby-pcap http grabbing example that lets you see the latest HTTP requests that have crossed a network card interface.

All so I can spy on my neighbors (but they can also spy on me).

Posted in , ,