Older blog entries for fxn (starting at number 477)

24 Nov 2007 (updated 25 Nov 2007 at 13:16 UTC) »

Conferencia Rails Hispana 2007

Back from a very successful Conferencia Rails Hispana 2007.

We were 200+ people this year, two days, three tracks. Technical talks, but also case studies and a round-table of companies with representants from Sun and Microsoft among others.

I gave my talk about Rails internals in the first morning. I liked very much the talk about plugins given by my colleague Javier Ramírez. He used funny and entertaining, but at the same time meaningful and inspired metaphors with Lego constructions on the stage, with real Lego stuff he brought in a box. There was a good introduction to Ruby metaprogramming by Sergio Gil as well, but unfortunately it had the same slot of another excellent talk by Pablo Delgado about scalability, performance, and all about measuring and applying common sense to your application setup. Sergio's was not being recorded so I attended that one, but I am looking forward to the video of Pablo's. REST, contributing to Rails, i18n, rspec, caching, ..., a wide range of topics were covered.

Thursday night there was an excellent dinner. It had the format of a cocktail, everyone standing up taking small pieces of food in a not so big room. That encouraged meeting people a lot more than sitting in a table. We had a really great time.

As it happens with good sysadmins, you know the work of the organizers is going well when you indeed don't notice them. Everything was smooth and well coordinated, kudos to the organization.

Obie Fernandez came to Madrid with his girlfriend. He gave an excellent keynote. In the first half he explained the most relevant changes in the upcoming Rails 2.0, and then there was a good discussion about team sizes, the future of Rails, etc. He gave his talk and answered the questions in Spanish, that was a challenge for him and he did it damn well. Right after the conference we took a taxi and went to Barcelona in the same plane, we were lucky a gentleman agreed to change his seat next to mine so that we could sit next to each other. We talked about a lot of stuff, from the old-times working in Java to ontolgies and OWL. He's a really really nice person, it was a real pleasure to meet him.

6 Nov 2007 (updated 6 Nov 2007 at 21:02 UTC) »

A humble contribution to Rails

In the upcoming Rails 2.0 there's a nice feature to be able to extract and DRY exception handling in controllers. If you declare this at the class level

  rescue_from SomeException, :with => :some_action

Rails catches SomeExceptions raised by any action and invokes some_action.

The initial implementation provided a useful feature but there was room for improvement. It used and underlying hash table with exception class names as keys and handlers as values. When an exception was being treated a table lookup by name was done. Thus, exception inheritance was not taken into account. That's important because a normal use case for that feature is to declare catchalls. Additionally, handler inheritance or priority were not expressable.

My coffee and I wrote a patch that adds those features and a few others the core team suggested it would be nice to have, and it is already in trunk.

3 Nov 2007 (updated 4 Nov 2007 at 11:27 UTC) »

Hyperreals

Between Perl and Ruby I try to find some time for math. As time passes I am more and more slow at digesting a page of stuff, but math is so beautiful! Reading a math book needs concentration and time to do exercises and think about what you learn to absorb it, which often gets interrupted by Real Life™ unfortunately.

I have founded an excellent book about a topic I wanted to read about sometime: Hyperreals. It is less than 300 pages long so I'll try to finish this one. Moreover the topic touches two areas I really enjoyed in the faculty: Mathematical Analysis and Mathematical Logic. (Well, to be honest I think I enjoyed everything except Statistics and Probability.)

A bit of History

Leibniz, Newton, and others in the 17th and 18th centuries use the ill-defined concept of infinitesimals. They base their math on these mysterious quantities, and do unknown things with them such as arithmetic, discarding terms when they are too small compared to other numbers, etc. Not very sound, but you know, the standard of rigour in math is a function of time :-).

So, epsilons and deltas weren't used originally. That's a formalism introduced in the 19th century by German mathematicians because at that time there were ambiguous definitions in the fundamentals of calculus and it was hard to move on in a confident way. Karl Weierstrass introduced the modern notion of rigour into Mathematical Analysis.

After the "refactoring" of the 19th infinitesimals were no longer used, everything was rigorously defined and proved in terms of epsilons and deltas, limit, continuity, differentiation, etc.

The Compactness Theorem

We jump now to the 20th century and the modern development of Mathematical Logic.

Without getting too specific, given a set of axioms we say that such mathematical entity is a model of them if the entity satisfies them. For example we say that the integers are a model of the axioms of a commutative ring. They are not a model of the axioms of a field, because there are non-null elements in Z that have no multiplicative inverse in Z.

Alright, there's a fundamental result in Mathematical Logic called Compactness Theorem that more or less says that if any finite subset of a (possibily infinite) set of axioms has a model, then the entire set of axioms has a model. We'd need to know Mathematical Logic to give a precise theorem but that idea is enough.

Take the axioms of an ordered field and add to them these:

Axiomn = there exists ε such that 0 < ε < 1/n.

Now, R and Q are models of any finite subset of these axioms, and it follows from the compactness theorem that there's a model of the entire set of axioms. That's an ordered field that has numbers strictly greater than zero and strictly less than 1/n for all n in N. There you have it, the infinitesimals back on scene.

A Construction Of The Hyperreals

Besides, that is not waving hands, the compactness theorem has a proof that actually constructs the model using a structure which is very used in logic called an ultraproduct . Reals can be constructed from the rationals using Dedekind cuts or equivalence classes of Cauchy sequences. I was lucky to see that construction in detail in my first year in the faculty. The Hyperreals are constructed as equivalence classes of real sequences modulus a nonprincipal ultrafilter on N. Two sequences are equivalent iff the set of indexes where their elements are equal belongs to the ultrafilter. The book motivates and goes through this construction. The existence of such a nonprincipal ultrafilter is not constructive though, it is proven using Zorn's Lemma.

The result is an ordered field, *R, that embeds the reals, has infinitesimals, and has as a consequence infinite numbers, greater than any real number: they are the multiplicative inverses of the infinitesimals the axioms of field guarantee. Properties of hyperreals are easily proven to hold in R thanks to the transfer principle, which informally explained says that any appropriately formulated statement is true of *R if and only if it is true of R. So you can work in *R and transfer the result to R by magic of the transfer principle. Of course the transfer principle has a precise meaning explained on Chapter 4.

A very important property of R for which the transfer principle cannot be applied is that R is complete, that is, every Cauchy sequence of real numbers converges in R. This had to happen because it is known that all complete ordered fields are isomorphic to R. So completeness is something we do not have in *R.

Non-Standard Analysis

This gave the chance to rewrite analysis using these contemporary infinitesimals with rigour. And this is what it is known as Non-Standard Analysis, which was introduced by the father of these ideas, Abraham Robinson.

The result is a beautiful and intuitive theory, where you can prove things talking about being infinitely close.

23 Sep 2007 (updated 23 Sep 2007 at 09:13 UTC) »

Entity Bodies in GET Requests?

I am reading RESTful Web Services, which nowadays is the book about the pure-REST, resource-oriented approach to Web Services.

The authors explain that albeit HTTP has no restriction about the length of the path of a GET request real web servers do have limits on what they accept. So, if you are designing an interface and some resource can have large scope data you need to take that into account. The proposed workaround is to forget purity and switch to POST.

HTTP/1.1 says about message-bodies:

The presence of a message-body in a request is signaled by the inclusion of a Content-Length or Transfer-Encoding header field in the request's message-headers. A message-body MUST NOT be included in a request if the specification of the request method (section 5.1.1) does not allow sending an entity-body in requests. A server SHOULD read and forward a message-body on any request; if the request method does not include defined semantics for an entity- body, then the message-body SHOULD be ignored when handling the request.
If you read section 5.1.1, and you follow to section 9.3 to read about GET, nothing is said about message-bodies. Hence, message-bodies are allowed in GET requests. In addition, if your web service documents the semantics of what expects in the message-body (scope data in such XML vocabulary, an image in such format) my interpretation is that all things are set up in a way that allows entity-bodies in GET requests.

Of course the authors of RESTful Web Services know their stuff, so I wonder why they recommend POST.

I guess the message-body of a GET method as per HTTP/1.1 has no defined semantics, and there's also this explicit mention to the path in section 9.3:

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.
which might imply everything needed to serve that request as far as the resource is concerned has to go there. Hmmmm....

21 Sep 2007 (updated 21 Sep 2007 at 22:35 UTC) »

Infinite Ranges in Ruby (cont.)

When I wrote yesterday that Ruby ranges may be infinite the first example that came to my mind as I was writing the mail in ruby-talk involved an enumeration of the rationals in [0, 1] and that somewhat obscured my point.

There's a more simple approach where you define <=> and succ in a class which consists of the naturals plus the first infinite ordinals ω, ω + 1, ω + 2, ... In that class 1 .. ω is an infinite range. But I didn't want to present this alternate proof because albeit I could skip set theory jargon altogether, using numbers past infinity was conceptually not a big gain over the previous reasoning.

Late at night I finally was able to construct an analogous argument to the one that uses ordinals, but just using Z × Z, which can be thought as a set of pairs of integers by any programmer and so it was much more understandable in the mailing list. This is the resulting class:

      class ZSquared
        include Comparable
    
        attr_reader :n, :m
    
        def initialize(n, m)
          @n, @m = n, m
        end
    
        def succ
          self.class.new(n+1, m)
        end
      
        def <=>(zs)
          [self.m, self.n] <=> [zs.m, zs.n]
        end
      end
which allows the definition of an infinite Range:
      ZSquared.new(0, 0) .. ZSquared.new(0, 1)

So yes, Ruby ranges may be infinite, and thus Range#length defined as a loop over succ wouldn't necessarily terminate.

20 Sep 2007 (updated 22 Sep 2007 at 11:34 UTC) »

Infinite Closed Ranges in Ruby

There was a question today in ruby-talk about whether Range#length could be defined by subtracting its endpoints.

The short answer is that such a definition is not guaranteed to work because objects in ranges are not required to respond to #-. Objects in ranges must respond to #<=> and #succ, and so you could in principle define Range#length iterating over the elements from the left endpoint to the right endpoint via #succ.

For completeness, I then noted that indeed those methods do not guarantee finite ranges (theoretically, real computers have physical constraints) and some discussion followed because, after Rick DeNatale asked for it, I gave a demonstration that used Q in a strange way (strange for people not exposed to this kind of math anyway). What surprises is that I define #succ in Q ∩ [0, 1], and albeit the logical steps are there, people believe the rationals per se are dense and so no #succ is possibly definable. That's not the case.

Density is a property of a set together with some order. Sets are orderable in different manners, sets are just collections of elements with no other property. If you define what does to be less than means in a given set, you are defining an order in that set. The definition is arbitrary, it is enough that it satisfies some properties. In Set Theory an order is just a subset of S × S that satisfies a few constraints, so it is something very generic. It models stuff we are used to, like the order in integers, but it is abstract.

OK, I claimed you could define suitables #<=> and #succ to enumerate the rationals in [0, 1] in a way that the Range 0 .. 1 is a well-defined infinite range.

The non-constructive proof uses a quite standard technique: Let f be a bijection between the rationals in the open interval (0, 1) and N. Such an f exists because Q is countable. For f(n) = q we define q.succ to be f(n + 1). And given f(n) = q, and f(m) = p we define q <=> p iff n <=> m.

Now, define C to be the class N, with ordinary order, together with those rationals with that order, and extend <=> among elements of both sets in the natural way. Let 0.succ be f(0), and n.succ be n + 1 for n in N, n > 0. The objects of class C implement the required interface, and 0 .. 1 is an infinite Range. You've got a #succ in the rationals of [0, 1]. You see, the rationals are not dense themselves, they are only dense with the ordinary way we order them.

I have somewhere in my notes from the faculty an explicit bijection between the rationals and the naturals (that was 1992, I indeed proved the bijection as an extra exercise), so a (again, theoretically) computable f could be indeed programmed in Ruby.

I think a much simpler example could be constructed taking N and a few infinities.

Update: The interface #<=> and #succ is required in the docs and in the Pickaxe, worded as as long as and must respond to respectively. David Black said there's enough evidence to say the docs are wrong. The idea is that you'd document instead that succ is required only if you are going to iterate over the range.

1 Sep 2007 (updated 1 Sep 2007 at 22:05 UTC) »

Segway

So yes, I finally purchased a beautiful brand new black Segway i2.

This machine is absolutely fascinating. I think one of the things that hooks you is that it is so intuitive, responsive, and stable, that you get a natural feedback loop when you glide on it, it becomes an extension of you.

Among other regular displacements like taking my daughter to school, I have 10 km of commuting in Barcelona, from the Camp Nou to Plaça Universitat and back, and have done it twice since I received the machine on Wednesday. I get there earlier than with public transportation, bike, or car. I don't sweat and can wear whatever I want because it is clean. And of course if you are the kind of guy that like riding things, you have a great lot of fun. Barcelona is very good for this kind of transportation because corners have inclines for pedestrians to cross streets.

The machine is so precise in the entire range of speed from 0 km/h to 20 km/h that you can move much much slower than a person walking. For instance you can go to a supermarket and move as you move in a supermarket. Stop, forward a bit, go back a little, turn towards the products, etc. I am testing the reactions and acceptance of people in several places and so far, I just receive positive, curious, and affective comments, if nothing at all because curiously you can move so much slow that some people just don't notice it, or does not get their attention. Those are interesting reactions so far because the main point is that they do not perceive a machine, they do not perceive something strange, mechanical, aggressive, out of place, if you use the Segway in a respectful way and try to integrate with the environment in my trials you are accepted naturally. Of course I won't take the Segway to go to the super or to buy bread, but I am testing it in a few places as an experiment, to discover the practical limitations and social acceptance.

The engines are so powerful that they take my 93 kg up in very inclined streets without any performance loss. And in those streets you can equally stop, turn, etc. with total control. You can approach inclines in any angle and stay vertically, I've ridden it in several kind of terrains and you feel secure, stable, no matter how irregular the way. You can jump over curbs without any special hability, the Segway takes care of it. The Segway can go downstairs (it inherits technology from the iBOT), and some people do it just fine, but the user manual does not recommend it.

Of course my colleagues have tested it, my father has ridden it, my wife takes it already, and I demo the machine to a lot of curious people in the street, the Segway is not as popular here as it is in USA. Most people have seen it in TV or movies but it is not very normal that you see a guy in a Segway here or in Hospitalet (I've just seen a couple of personal Segways in my life here, not counting the tourist tours), so a lot of people approach you in the street and ask about it.

I could finance it as a vehicle for my professional transportation and that gives a somewhat cheaper price in practice because you can discount it for taxes.

Well, I am really excited about it, it promises years of fun.

14 Jul 2007 (updated 28 Dec 2007 at 16:18 UTC) »

Algorithm::Combinatorics

I published version 0.24 of Algorithm::Combinatorics today, which provides subsets().

Link generation for websites with SSL in Rails

Secure Actions is a helpful plugin that facilitates URL generation for websites that use SSL. The plugin provides a class method for controllers to declare their secure actions:

  class AccountController < ApplicationController
    include SecureActions
    require_ssl :login, :signup
  end
and that's enough to get URL generation with the right protocol. To accomplish that the plugin implements a hook Rails invokes at the beginning of URL generation in url_for. That hook is called default_url_options and receives the hash used in the view, say:
  <% form_tag :controller => 'account', :action => 
'login' do %>
  <% end %>
and so a table lookup is enough to determine the protocol needed by that URL. By default URLs in images, JavaScript includes, etc. are absolute paths in Rails, so with zero effort included resources inherit the right protocol as well.

The plugin is enabled via a global flag USE_SSL, so that you can turn SSL on just in production mode for example.

As of this writing there's no way to declare an entire controller to be secure, I wrote this trick in application.rb:

  # A controller makes this call to declare all their actions run behind SSL.
  # The call must be put at the bottom of the code, so that the public methods
  # are known and returned by public_instance_methods.
  def self.this_controller_only_responds_to_https
    include SecureActions
    require_ssl *self.public_instance_methods(false).map(&:to_sym)
  end
That way, you can declare everything to be secure in one shot:
  class BankAccountController < ApplicationController
    ...
    this_controller_only_responds_to_https
  end
Note that we need to put that call at the bottom of the class definition so all methods are known, I don't like such an important declaration not to be at the top, but the technique requires it that way.

Of course, if the controller has some magic via method_missing or whatever that won't be enough, but in that case you probably know what to do. Also controller inheritance trees may need some tweaking for inherited actions. Some simple array arithmetic will probably suffice in that case. You don't need to be precise nonetheless, the plugin just builds a hash table that maps symbols, so if :object_id ends up there it won't hurt, you are not going to link to BankAccountController#object_id anyway.

I found an interesting gotcha using this plugin: In Rails automatic class loading is implemented via const_missing. That is, when you try to use an unknown class that Ruby hook is triggered, and Rails looks up the class definition in the application tree according to well-known conventions. For instance, if the callback is called because you try to use UserController, Rails tries to require user_controller.rb. Since RAILS_ROOT/app/controllers belongs to $LOAD_PATH the file is executed and the controller loaded. In production mode that's done once, and in development mode that's done per request because of the way class reloading is implemented.

Right, suppose you have a link to a login form /account/login in the non-secure home of your application /public/index. That link should be secure, but when you launch the server nobody has yet hit anything related to the AccountController so that class is not yet loaded. The plugin knows nothing about the secure actions of the controller in consequence, and therefore generates a non-secure link. The first login in that setup goes in plain. That argument applies to the entire website, until a controller is loaded the plugin knows nothing about his secure actions.

To fix this I force controller loading in environment.rb with this little code:

  # Trigger controller class loading to execute SSL-related
  # declarations, this way we have the correct links right away.
  require 'application'
  ActionController::Routing.possible_controllers.each do |c|
    # known to work without directories
    "#{c.camelize}Controller".constantize
  end

15 Jun 2007 (updated 15 Jun 2007 at 12:46 UTC) »

I had lunch yesterday with jao to say goodbye to him before he moves. He is about to join Google Zurich. I am very proud of him, he has worked hard for years and deserves that job. Good luck dude!

29 May 2007 (updated 29 May 2007 at 11:16 UTC) »

Segway Tour

After several months using the Monty DPie on a regular basis I know I like this kind of machines for transportation.

The Monty DPie is cheap, kind of a toy, three wheels, not much autonomy, not enough power for real, everyday urban transportation, ..., and was a way to test whether I'd actually use such a thing with small risk in money. If it turned out it didn't work it wasn't a big deal. I have had a great time riding it, and great times with my daughter. And well, the actual usage has gone much further than I expected, so the next step is to buy a Segway as soon as I can afford it.

I could enumerate a list of reasons why I like these machines, no maintenance (a Segway costs 0.01 € per km, that's 30 € a year with regular usage), they are clean, agile, ..., but the most important one for me is that transportation becomes fun. That's the point, fun.

Of course before you buy a Segway you'd better test one and see whether it's for you. So this weekend my wife and I took a Barcelona tourist tour in HTs. Two hours and a half, about 10 kms. Man, that machine is really, really impressive. The easy of use, the astonishing stability, the robustness, ..., really outstanding. So yes, test passed wih grade. This review reflects very accurately our experience, it feels like that.

Let's see whether some day I can blog I own one of those :-).

468 older entries...

New Advogato Features

FOAF updates: Trust rankings are now exported, making the data available to other users and websites. An external FOAF URI has been added, allowing users to link to an additional FOAF file.

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!