Ruby, Unicode and Tcl

Apparently Tim Bray gave a talk at RubyConf about “RubyConf: I18n, M17n, Unicode, and all that”, which discussed the approaches other languages take. It’s a pity that Tcl wasn’t mentioned, as it basically gets things right. It is fully unicode aware, and generally “just works”, because all strings are unicode strings, by default – there isn’t a separate syntax, or commands to create and manipulate multibyte strings and characters.

% set c u0065
e
% string bytelength $c
1
% string length $c
1

% set c u2022
•
% string bytelength $c
3
% string length $c
1

That works for regular expressions, too.

rbatis

I discovered rbatis at work this afternoon. It’s clunky compared to activerecord, but that isn’t the point. What it potentially gives you is the possibility to interact with ugly legacy databases – or even just legacy tables. It’s pretty new, and my bet is that with some work, it would be pretty easy to automate some fairly decent scaffolding for it in order to take most of the grunt work out of it.

Generic Rails Search

Once again, I’m not involved enough in the rails community to know if something like this has already been done, but I thought it was kind of cool. It’s a generic search system for ActiveRecords:

def searcher_find(paramname, model)
  conds = []
  condvals = []
  columns = model.column_names
  params[paramname].each do |k, v|
    ks = k.to_s
    if columns.include? ks

      col = model.columns_hash[ks]
      case col.type
      when :text
        conds << " #{k} like ? "
        condvals << "%#{v}%"
      when :integer
        if v != ""
          conds << " #{k} = ? "
          condvals << v.to_i
        end
      when :float
        if v != ""
          conds << " #{k} = ? "
          condvals << v == v.to_f
        end
      end

    end
  end

  return model.find(:all, :conditions => [conds.join(' and '), *condvals])
end

You would call it like so: @products = searcher_find(:product, Product). The above version is just the “simplest thing that could possibly work”, and probably needs fixing, but I thought I’d write it down so that others could take it and use it.

Practical Agile?

At work, we’ve decided we need a little bit more process, so I’m going to attempt to lead our small group of programmers in that direction. We have a vague feeling that since there aren’t many of us, we need to keep things as lightweight and practical as possible, and that something “agile” would probably be the best direction. Beyond having read a bit here and there, I’ve never had any direct contact with “agile” in the wild. We don’t want a rigid methodology so much as a set of guidelines and, perhaps, tools that help us track time and projects. It all needs to impose very low overhead – because an imperfect tool that gets used is far better than something very fancy that gets ignored when people get in a rush.

So… advice? I’m willing to get a book or two, but I have a sneaking suspicion that many of them are quite inflated with respect to their core concepts which could likely be summed up in a few pages. Anyone have any practical guides for small shops that want to try and reduce the chaos level just a bit?

Knuth Bug, Call by ?, OpenExp

A few interesting things:

A bug in Knuth’s Art of Computer Programming

I opened it up yesterday to see if he had anything relevant to some code I was writing, and a bug crawled out (and I squashed it). I thought to myself that it’s not often that people find bugs in Knuth’s books…

Hecl and SourceForge

We’re still debating how Hecl ought to work in terms of call by {reference/name/value/whatever}, which has proved to be very interesting, and require some healthy mental gymnastics to think about it all. Unfortunately, as of late, SourceForge has been rejecting email from gmail. How annoying is that?!

Hecl @ OpenExp

I want to the OpenExp conference in Cerea on Sunday to give a talk about Hecl. That area of Italy is sort of like the Nebraska of Italy. Wide, flat, and not a whole lot there (although this being northern Italy there are still lots of little towns). In any case, the talk when ok, although I don’t think I got quite as many people as I could of… it’s frustrating trying to think of a title or some means to convey to people that here is a technology they can use to do something cool (write programs for their cell phones) very quickly and easily. The people that were there seemed to “get it”, though.

openexp

Tomorrow, Sunday the 1st of October, I’ll be at the openexp event to talk about Hecl. Hopefully I’ll also get a chance to catch up with various people in the Italian open source scene who will likely be there.

Who calls what how?

We’re having quite an interesting discussion on the Hecl mailing list about the semantics and syntax of the language (since it’s in its early days, we get to do that!). I have some questions for those of you reading this, if you have time to comment.

Hecl inherits a lot of things from Tcl, which is both a simple, and amazingly flexible language. One of the things that Tcl lets you do is modify the value of a variable. For instance, set x [list 1 2 3] ; lappend x 4;. However, what is at times confusing for people is that to get the value of a variable, you place a dollar sign in front of the variable: lindex $x 0 would return 1 for instance. To some people this is all quite intuitive, because commands that modify the variable take its name, which is then looked up in a hash table and fiddled with as needs be. So we have constructs like if { $n == 10 } { incr n }. To other people, especially those who come from the Perl and PHP worlds, variables always have a $ in front of them, so the Tcl way of doing things doesn’t make as much sense. Even for experienced programmers, it can at times require a moment to recall how a particular command works. lappend foo ; lrange $foo ; lset foo ; lindex $foo and so on. On the plus side, this is all very easy to implement, and really isn’t that difficult to figure out.

One avenue I’ve pursued with Hecl is to be able to pass references around, instead of variable names, so that all commands just take “a variable”: lappend $foo x instead of Tcl’s lappend foo x, and so on. For the moment, I’ve also enabled proc’s (user defined commands/functions) to receive references to variables passed to them, so they can modify them. This is both powerful, and dangerous:

set x 1
proc foo {v} {
    set v [* $v 10]
}
foo $x
puts "foo is $x" ;# $x is now 10

(Well, not exactly, but that’s the general idea). We are considering ideas like marking the variables that may be modified in the proc definition to reduce the danger of people shooting themselves in the foot. In any case, it increases consistency, because commands that modify values always take a $variable. That still leaves commands like set foo 10 (others are proc, foreach, and catch) that take variable names to be created, rather than $variable $references, so perhaps to someone who comes from the world of PHP, it’s still not as clear as it could be.

We’re also considering some radical changes, for the sake of not excluding any particular direction: taking a more lisp/ruby/python like approach, and not using dollar signs for variables (except perhaps in string interpolations). This has some slight disadvantages in terms of requiring quotes around more things (in Tcl, you don’t need to quote single world strings). This might result in code that looks something like this:

proc test {name code result} {
   catch code res
   if { eq res result } {
       ok name
   } else {
       fail name res result
   }
}

proc testfiles {files} {
   global ok
   global failed
   # clear success/error list
   set ok [list]
   set failed [list]
   foreach f files {
   puts "Running $f"
       source f
   }
}

Which isn’t bad, although in some places, I think the added syntax of $ helps to make it clearer, at a glance, what’s what. eq $res $result is just that much quicker to pick out. This is especially true if you stash a command name in a variable.

In other words, set p puts ; $p "hello world" might be a bit a bit clearer than set p "puts"; p "hello world", but… it’s definitely “eye of the beholder” territory, and depends on what you’re used to!

In any case, it’s been a very interesting process, and I’ve greatly enjoyed the company of Alexandre Ferrieux and Wolfgang Kechel on the list, as well as my friend Salvatore Sanfilippo. They’re all very bright guys with good ideas, which makes for lots of thought-provoking discussions.

Where you come in, if you’ve made it this far… is what kind of syntax and semantics you prefer, and why? Call by name? By reference? Modifiable values? Tell me what you think!

Rails pagination with N results

For a site I’m building with Rails, I wanted to paginate the results of a database query with the built in pagination utilities. However, out of the box, paginate doesn’t take a :limit option, because that’s used to get a certain number of results per page. This is the workaround I put together. Perhaps there are better ways, but this was quick and efficient.

In the controller:

class StoreController < ApplicationController

   # This is for Pagination.
   DEFAULT_OPTIONS[:first_n] = nil

This lets you use a new option to paginate, :first_n (which isn’t the best name, but was the first thing that popped into my head).

Now, you override this method:

  def count_collection_for_pagination(model, options)
    unless options[:first_n].nil?
      return options[:first_n]
    else
      return model.count(:conditions => options[:conditions],
                         :joins => options[:join] || options[:joins],
                         :include => options[:include],
                         :select => options[:count])
    end
  end

So, if you only want N results, you then do:

@prod_pages, @products = paginate(:products, :condition => condition, :first_n => 100)

Seems to be pretty quick and simple, so it works for me, although I’m sure a Ruby expert could make it look even nicer.

Preliminary MIDP2.0 Support for Hecl

Wolfgang added preliminary support for MIDP2.0 to Hecl this afternoon. It’s exciting news, because it’s a big chunk of very useful code – MIDP2.0 is what most fancier phones have – in other words, it’s what other geeks are running. Hopefully this will increase Hecl’s appeal to that crowd. Naturally, the code was just checked in, so it’s probably only suitable for those willing to hack on it and get involved right now, but hopefully we will cut a new release that includes it “soonish”, depending on the amount of free time I have.

Thanks!

American Economics Association

It’s obvious from my writings that I have developed an interest in economics, which developed out of a curiousity about the workings of free software business – how were we to make money creating free software? While that’s still an open question, the tangent has been a fascinating one that I’m glad I followed. My work with the Tcl programming language further encouraged my interest as I sought to reconcile the language’s extremely high quality and versatility with its falling popularity. If the answer wasn’t a technical one, it had to be some squishy, human factor… economics and marketing.

In any case, one of the things that has frustrated me about economics compared with my native territory of free software is the relative paucity of on line information, and almost total lack of quality sources of feedback (discussion groups or the like where serious discourse is the rule, rather than quackery or debates that quickly degrade into political rants). Not having the time, nor at present the inclination to sidetrack to the point of pursuing a degree in economics, which is the most obvious way of coming into contact with a lot of thought on the subject, at the recommendation of professor Stephen J Turnbull, I signed up to the American Economic Association, which sends out several hefty academic journals every quarter.

Some of the math is a bit daunting, but there is quite a bit of material on various topics, and I think many others (there seem to be a fair amount of open source people with at least a passing interest in business and econ) who want to see something “closer to the metal” would find these journals worth their while, and it’s not very expensive to sign up.