Thursday, April 03, 2008

Draft of Erector talk

I'm giving a talk on erector in about a week at the Washington, DC Ruby User's Group. Here's a draft of my talk; please provide feedback so I can improve the talk (for example, as comments at the bottom of this page).



Ruby's flexible and minimal syntax makes it well-suited to writing libraries for various tasks


Erector applies that to HTML (or XML) rendering


(start up erector in script/console and go through the following examples)

class Foo < Erector::Widget
def render
html
end
end
Foo.new().to_s


Erector::Widget.new() do
html
end.to_s


Erector::Widget.new() do
p "foo"
end.to_s


Erector::Widget.new() do
text "hello"
end.to_s


Erector::Widget.new() do
p do
text "hello"
b "world"
end
end.to_s


Erector::Widget.new() do
a :href => "a.html" do
text "world"
end
end.to_s


Erector::Widget.new() do
a "world", :href => "a.html" do
end.to_s


Erector::Widget.new() do
text '<>&'
end.to_s

Hooking erector to rails:


class WelcomeController <
ApplicationController

def index
render :text =>
Views::Welcome::Show.new().to_s
end

end

Structuring views with the usual Ruby techniques: especially inheritance and methods


Responsibilities of a view mechanism:


  • quote output


  • balance start/end tags





Comparison with ERB and Markaby, quoting:

ERB: no, must call h

Markaby: for strings, not blocks

Erector: yes*

* except when you call raw


Comparison with ERB and Markaby, tag balancing:

ERB: no

Markaby: yes

Erector: yes*

* except when you call raw


Comparison with ERB and Markaby, subpages:

ERB: partials, helpers

Markaby: partials, helpers

Erector: inheritance, methods


Similar libraries in other languages:




If there is time:

  • Calling helpers from erector

  • Writing helpers in erector

Wednesday, February 20, 2008

Hello world type example for jruby and mayfly

Given all the interest in the ruby programming language, it is natural to ask whether rails applications (or ruby applications more generally) could write their tests with mayfly.

As a first step, I figured out how to run mayfly under jruby. Here's what I did.

First, I installed jruby JRuby 1.1 RC 2as directed. Then, I put the mayfly 0.3 jars in $JRUBY_HOME/lib (there are other ways to tell jruby where to look for them, but this seemed like the simplest).

Then, I put the following in hellodb.rb:


include Java
import 'net.sourceforge.mayfly.Database'

d = Database.new()
d.execute("create table foo(x integer)")
d.execute("insert into foo(x) values(5)")
puts d.rowCount("foo")
d.execute("insert into foo(x) values(6)")
puts d.rowCount("foo")


Then running jruby will invoke mayfly:


$ jruby hellodb.rb
1
2


The next step would be to figure out how to get Active Record talking to Mayfly.
Haven't tried that yet.

Monday, February 11, 2008

Profiling with gprof (success on a short test program)

When last we discussed profiling Mayfly, I was profiling with JIP. Brian Slesinsky, in a comment to that article, told me that he has found that JIP has a per-method cost which shows up in the profiling data (so that method calls appear to be more expensive than they really are). He suggested the NetBeans profiler.

Well, I was looking into how to install and use NetBeans (NetBeans, unlike Eclipse, does not ship with Fedora), and hadn't gotten much of anywhere until I had a lot of time (on a flight), and was left with seeng whether I could get anywhere with the tools which I have already installed. That means gcj and gprof. I got gprof working fine on a short test program (where it correctly identified the bottleneck), but didn't (yet) succeed in running it on mayfly.

I suppose if people would find them helpful, I could upload my test programs and build scripts, but basically they boiled down to:


gcj --main=Profilee -pg Profilee.java
./a.out
gprof >profiler


Getting something like this invoked from ant is largely a boring but more or less straightforward matter (although it wasn't clear to me how the classpath relates to the class and java files specified on the command line). But at least one of my invocations led to a runaway linker which made my machine swap for quite a while before I finally gave up. So although it is premature to declare victory on this just yet, I did want to report on my success with the test program.