[SVN] Supporting Ruby 1.8 and Ruby 1.9

James Gray james at grayproductions.net
Mon Sep 8 21:54:22 UTC 2008


In applying the recent 1.9 support fixes from Charles Turner, a  
pattern is emerging.  I'm adding several checks and switching  
strategies based on what is supported.

An example of this is String iteration.  In Ruby 1.8 you can iterate  
over the lines of a String with something like:

   str.each|map|etc. { |line| … }

In 1.9, Strings no longer mix-in Enumerable.  Instead, you must  
explicitly say how you want to divide up the String (lines, bytes, or  
characters) and then iterate:

   str.lines.each|map|etc. { |line| … }

To solve this, I've been checking for lines() and calling it when  
available.  When it's not, I replace the call with to_s() (effectively  
a no-op) and iterate over the result:

   str.send(str.respond_to?(:lines) ? :lines : :to_s).each|map|etc. { | 
line| … }

We now have several of these checks in the Ruby code TextMate uses.

My question then is:  should we come up with a more general solution  
for this?

For example, we could build a 1.9 shim library that either adds a  
suitable lines() on 1.8 or creates a new method altogether that uses  
the proper strategy on each platform.  We could then require that  
library and try to stick to using it for the stuff we need to work in  
both places.

I guess that's not ideal, because we must be diligent with the extra  
require and usage of new methods and many contributors just may not  
know to do this.  Thus, I'm open to other suggestions.

Again though, my question is:  do we want to formalize some strategy  
for supporting Ruby 1.8 and 1.9?

A separate but related issue is how we are going to handle 1.9's  
encoding engine.  Essentially, we need to know the data's encoding in  
1.9 to work with it correctly.  Does TextMate give us anyway to know  
the encoding of the current file?  Would it be possible to get an  
environment variable for that?

I guess we could also treat everything as UTF-8 or binary and just let  
things explode when that's not the case, but that seems pretty  
draconian.

I'm open to suggestions here as well, of course.

James Edward Gray II




More information about the textmate-dev mailing list