Older blog entries for fxn (starting at number 445)

Advogato Backups

In case you didn't come across this before, the entire diary of any account is available at http://advogato.org/person/<account>/diary.xml.

I consider just a tiny part of my disk to be worth being kept (life is ephemeral!). A trivial shell script fetches that XML and uses pax(1) to put it together with some selected stuff into a single PAX file. The resulting archive is transferred to a memory stick I always carry with me and that's all. Because of its simplicity I even do backups regularly! Incredible!

Understanding newlines in Perl

After some email exchanging with Jarkko and Sadahiro, plus my own reading, I got the picture to write a final section about newlines in Unicode (in the context of Perl) that closes my article. That's the first complete draft, and I sent it yesterday to chromatic. I like the article!

Ruby on Rails tutorial

I'll give a tutorial about Ruby on Rails in the next V Jornades de Programari Lliure. It will be based on the one I gave a few weeks before at the University of Barcelona in cerquide's course. Since the slot is two hours here, a bit longer, I'll be able to spend more time on Ajax integration.

4 Jun 2006 (updated 5 Jun 2006 at 07:28 UTC) »

SQLite at Google

Dr. Hipp gave recently an introductory talk about SQLite at Google that was recorded in video.

Circular Permutations

I just uploaded to PAUSE a new version of Algorithm::Combinatorics with a generator of circular permutations. That was a breeze to implement fixing the first element and delegating to permutations the generation of arrangements of the rest. Additionally, that's a choice of representants that gives lexicographic order.

I noticed the sequence of numbers of circular permutations has no entry in Sloane's encyclopedia, and so I submitted it tonight.

3 Jun 2006 (updated 3 Jun 2006 at 11:51 UTC) »

Show/Hide with CSS and JavaScript

My diary here has been mostly descriptive and with short entries so far: I've done this and that. I am reading such and such.

One of the reasons for that is that I've always seen my diary at Advogato like a diary, rather than a blog. Surely because that's the term we use in Advogato: diary. The main reason though is that I don't want to throw a ton of stuff to recentlog, which is a page most regulars read.

Lately I've been reading a bit about CSS and JavaScript and it just occurred to me show/hide links can be written with the allowed markup. I used this yesterday for the first time, the trick is simple:

  <p><b>Entry Title</b></p>
  <p>This is the visible introduction.</p>
  <div id="rest_of_nick_entry" style="display: none;">
    <p>Here starts the rest which is toggleable.</p>
  </div>
  <p><a href="#" onclick='e=document.getElementById("rest_of_nick_entry"); e.style.display=(e.style.display=="none"?"block":"none");return false;'>[ Toggle ]</a></p>

We put the toggleable part of the entry in its own div with style display: none. That tells the browser to remove that block altogether from the flow of the page, so it is not seen and the space it occupies is not taken into account. As if it didn't exist.

After the closing </div> of the toggeable section we put a link with an onclick handler that toggles the display property with JavaScript. Browsers monitor those changes and apply the new style on the fly. The handler returns false because that way the browser ignores href and the page does not move. We are done!

I think the first time I saw this trick, or the first one I noticed its use clearly, was reading the documentation of Rails. That's something similar to Javadoc where each documented method has a link to show/hide its implementation. Really cool, I use that a lot.

Having this trick at hand I think my diary is likely to become a bit closer to a blog.

1 Jun 2006 (updated 18 Aug 2010 at 11:47 UTC) »

Dissecting the Rails History plugin, or a little metaprogramming by example

The Rails History plugin was announced today in the Rails mailing list. It is a simple plugin that has the virtue of doing its job well and idiomatically. It keeps a queue of visits per session, except for POST and Ajax requests, that is available to controllers. Makes for a perfect exercise on studying some source code.

Let's see the usage. Once you install this plugin, in order to enable session history is enough to do this:


  class ApplicationController < ActionController::Base
    history
  end
Yeah that's all. Optional parameters for a default URL and the queue size are provided:

  class ApplicationController < ActionController::Base
    history :default => "http://example.com/", :max => 10
  end

Right, now we have automagically history support in all controllers, who inherit a few instance methods to use the queue like redirect_back for example:


  def some_action
    # do stuff
    if some_condition
      redirect_back(2) # go two requests back
    end
  end

The plugin does more things, but essentially that's enough to depict the techniques it is based upon. We have:

  • New class methods defined for ApplicationController
  • New instance methods available to controllers
  • History housekeeping maintained transparently

Any Rails developer will be familiar with that way of getting new features.

The plugin defines a module History who has a class Container, and two convenience modules ClassMethods, and InstanceMethods.

The module ClassMethods contains the class method history, and the module InstanceMethods groups the instance methods like redirect_back. How do we add them to ActionController::Base? Easily with Ruby metaprogramming features:


  module History
    def self.append_features(base)
      super
      base.extend(ClassMethods)
    end
    # rest of code
  end
When a module is included in another Ruby calls append_features, passing the receiving module in base. There we extend it with the class methods wrapped in the ClassMethods convenience module, which happens to define history. This top- level code does the mixin:

  ActionController::Base.class_eval do
    cattr_accessor :history_container
    history_container = History::Container.new
    logger.debug("loading history")
    include History
  end

The Container class implements the history manager itself so to speak. The method class_eval executes that block of code in the context of its invocant, which is the class ActionController::Base. So we are creating a class accessor for history_container, creating an instance, and including the mixin History. This last step is the one that makes history available as a class method in ActionController::Base.

Now, how are the instance methods available. The trick here is done in the very history method:


  def history(options)
    logger.debug("history: setting up history")
    include History::InstanceMethods
    class_eval do
      ActionController::Base.history_container = History::Container.new(options)
      after_filter :store_location
    end
  end
As you see that method mixins the instance methods, instantiates a new container with the received options, and puts the method store_location as an after filter. That filter does the housekeeping and that's why you don't notice.
23 May 2006 (updated 23 May 2006 at 16:33 UTC) »

Algorithm::Combinatorics

I've published a new version of Algorithm::Combinatorics that provides generation of set partitions, and set partitions of a given size. Their implementation, with a C core as usual in that module, follow an approach based on restricted growth strings, transcribed from this paper by Michael Orlov.

Article about newlines in Perl

My friend Enrique Nell, who is a professional software translator, proof-readed the current draft and improved the English here and there. I have done some cosmetic changes today, and wrote to Jarkko about Unicode.

19 May 2006 (updated 19 May 2006 at 06:38 UTC) »

Rails talk

I gave an introductory talk about Rails at the University of Barcelona this week. It was an invited talk within cerquide's course on network programming, though attendance was open. It was in fact a very nice surprise to see there some friends, ex-coworkers, and collegues.

The talk was a mixin of a few text-based slides and the development of a simple application that could demonstrate how one works in Rails, explaining, you know, MVC in Rails, migrations, generators, persistence, Ajax, etc., as we went along.

Article about newlines in Perl

I have no response as of today from that expert on Unicode, this weekend I'll move this forward somehow.

6 May 2006 (updated 6 May 2006 at 08:51 UTC) »

I am refining the draft of the article about newlines in Perl.

In the context of Unicode looks like one has to modify his programming habits: According to the Unicode Newline Guidelines Unicode-aware programs should accept any kind of line terminator, including NEL or LS for example, and states that PSs and LSs "should be used wherever the desired function is unambiguous". My interpretation of that sentence is that if a program is writing to a log file using Unicode and it ends traces with a newline, a LS should be used instead of the traditional \n. Is anybody doing such a thing?

Obviously that stuff needs real-world experience I lack to put things in the right context, give advice on good practices, etc. The draft contains nothing about Unicode by now, I invited an expert to contribute such a section.

2 May 2006 (updated 2 May 2006 at 17:26 UTC) »

Talking about newlines, there is a misleading comment in the section about newlines in perlport.pod. The argument goes one shouldn't use chop to trim newlines in standard <>-loops because in CRLF platforms it would delete only half of it.

That is not correct, in CRLF platforms the Perl I/O layer translates in text mode CRLF to LF on reading, and the other way around on writing. I sent a patch that Rafael applied right away.

436 older entries...

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!