Archive for the 'Coding' Category

Continuations in Ruby

December 12, 2007

A week ago, Dr. Frens spoke about continuations in Ruby at the Michigan Ruby Users Group. It was very interesting as it brought back my college days when I learned Scheme and functional programming concepts. I always enjoyed functional programming because it presents such a different way of thinking through problems.

I was going to blog about his presentation, but it turns out he blogged all his own notes already. But what I found interesting is the sort of hybrid nature that Ruby has. It is both an object-oriented language and it also supports some functional programming concepts. The drawback Dr. Frens found was that Ruby doesn’t do tail recursion optimization, so it’s not really worth using continuation passing style functions. Interesting, nonetheless!

Eager Loading

December 1, 2007

One thing that made me uneasy about Rails’ ActiveRecord was how it handles associations. For instance, say I have a table of products in the database and another table listing comments about the products. So, for each product we have many comments.

Back in the good ol’ ColdFusion days, I would write a query that joins the two tables. Then I would loop through each field and have some funky conditional statements, like the following:

<cfset prevProductId = 0 />
<cfloop query=”qryProductsWithComments”>
 <cfif product_id neq prevProductId>
  …if this is not the first product, finish printing the previous product’s template…
  …we’ve hit either the first product or a new product, so print out the beginning of the product template and the first comment…
 <cfelse>
  …this is another comment, so print out just another comment…
 </cfif>
 <cfset prevProductId = product_id />
</cfloop>

That’s pretty darn confusing, but it was better, performance-wise, than executing a new query every time you iterate over the products loop, like so:

<cfloop query=”qryProducts”>
 …spit out the product template…
 <cfquery name=”qryComments” …>
  SELECT * FROM comments WHERE product_id = #qryProducts.product_id#
 </cfquery>
 <cfloop query=”qryComments”>
  …spit out a comment…
 </cfloop>
</cfloop>

By default, ActiveRecord will perform similarly to the latter example. However, with eager loading, you can get the same performance as the former example without all of the confusing if statements:


# in our model:
class Product < ActiveRecord::Base
 has_many :comments
end


# in our controller:
class ProductsController < ApplicationController
 def index
  @products = Product.find(:all, :include => :comments)
 end
end


# in our view:
<% for product in @products -%>
 <%= render :partial => “products/product” %>
 <% for comment in @products.comments -%>
  <%= render :partial => “comments/comment” %>
 <% end -%>
<% end -%>

So much cleaner! And easier!

With ASP.NET 2.0, you’re pretty much stuck doing the equivalent of the second CF example using the server controls or you have to build your own object data source that will work similarly to ActiveRecord, but you’ve just spent an hour or so reinventing the wheel.

Some Very Strange Markup

November 7, 2007

I was viewing the source of the Facebook iPhone webapp to see how they did their fancy transitions between pages, when I found some very strange HTML:

<body home=”home.php”>
<div class=”toolbar1 tops chrome”>
<u href=”home.php”><img src=”images/facebook.png”/></u>
<u class=”button plain leftButton” href=”#status”>Status</u>
<u class=”button plain” href=”#search” radio=”true”>Search</u>
</div>

How can a <body> tag have a “home” attribute or a <u> tag have an “href” attribute? I scoured the W3C spec and looked for a possible mobile HTML standard supporting it, but couldn’t find anything.Well, it turns out they’re doing some non-standard trickery by adding custom attributes to their elements. They’re then using JavaScript to get the values of those attributes and use them in their “onclick” handlers for each element. Sneaky!

Fun With Microformats

October 24, 2007

I just updated my food blog by encoding all of the restaurants’ contact information in microformats. It’s basically a way to encode vCard/vCal data using HTML. It’s part of the latest movement to bring semantics (meaningful data that can be interpreted by computer programs) to web content. For instance, with this technology you could easily write a program to find all the phone numbers and addresses listed on the internet and generate a worldwide phone book. Or you could track who has lived in a given house over the past X years. Or you could generate statistics on how many CEOs live next to golf courses. Or you could figure out, according to published calendars, when most people will be away from home and thus not watching the latest sitcom. And that’s just the beginning.

One thing that’s cool about the hCard specification is its flexibility. According to the spec, they recommend the following format:

<div class="vcard">
  <span class="fn n">
     <a class="url" href="http://t37.net">
       <span class="given-name">Fréderic</span>
       <span class="family-name">de Villamil</span>
     </a>
  </span>
  <span class="nickname">neuro</span>
  <a class="email" href="mailto:neuroNOSPAM@t37.net">
     <span class="type">pref</span><span>erred email</span>
  </a>
  <span class="org">Omatis</span>
  <span class="adr">
     <abbr class="type" title="dom">France</abbr>
     <span class="type">home</span> address
     <abbr class="type" title="postal">mail</abbr> and
     <abbr class="type" title="parcel">shipments</abbr>:
     <span class="street-address">12 rue Danton</span>
     <span class="locality">Le Kremlin-Bicetre</span>
     <span class="postal-code">94270</span>
     <span class="country-name">France</span>
  </span>
</div>

But I was able to easily use different elements in place of <div> and <span>:

<p class="vcard">
  <span class="fn n">
     <a class="url" href="http://t37.net">
       <span class="given-name">Fréderic</span>
       <span class="family-name">de Villamil</span>
     </a>
  </span>
  <span class="nickname">neuro</span>
  <a class="email" href="mailto:neuroNOSPAM@t37.net">
     <span class="type">pref</span><span>erred email</span>
  </a>
  <span class="org">Omatis</span>
  <a class="adr" href="http://somegooglemapslink">
     <abbr class="type" title="dom">France</abbr>
     <span class="type">home</span> address
     <abbr class="type" title="postal">mail</abbr> and
     <abbr class="type" title="parcel">shipments</abbr>:
     <span class="street-address">12 rue Danton</span>
     <span class="locality">Le Kremlin-Bicetre</span>
     <span class="postal-code">94270</span>
     <span class="country-name">France</span>
  </a>
</p>

This was useful because wordpress does not allow <div> in your blog posts. It automatically converts them to <p> :p