tag:blogger.com,1999:blog-94690172024-03-12T18:07:43.557-07:00split-smartin's weblogMartinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.comBlogger49125tag:blogger.com,1999:blog-9469017.post-89884155783260596432007-08-22T15:16:00.000-07:002007-08-22T19:28:35.679-07:00Helping javac infer generic return typesOne of my pet peeves with Java generics is that you can't pass the result of a method that returns a generic type whose bounds cannot be inferred from actual arguments directly to another method. This is particularly useful when passing an empty list obtained from Collections.emptyList() to a method:<br /><br /><div class="code">class Test <br /> {<br /> static void foo(List<String> list) {}<br /> <br /> public static void main(String[] args)<br /> {<br /> foo(Collections.emptyList()); // doesn't compile<br /> }<br /> }<br /></div><br /><br />Unfortunately, that doesn't compile. It can be worked around by doing:<br /><br /><div class="code">List<String> list = Collections.emptyList();<br /> foo(list);<br /></div><br /><br />It beats me why the compiler cannot infer the appropriate type for the list returned by Collections.emptyList() in the first example above. I understand why it would fail when 'foo' has multiple overloads, but it should work for the simple case when the method resolution is unambiguous.<br /><br />I recently <a href="http://groups.google.com/group/google-guice/msg/2f4a4b7b30b067ba">came across</a> an <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12">obscure</a> (yes, ugly and unintuitive) way of getting the first example to work. It consists in telling the compiler explicitly what to bind the return type to. This is the BNF definition, per the <a href="http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html">JLS 3.0</a>:<br /><br /><div class="code">MethodInvocation:<br /> MethodName ( ArgumentList? )<br /> Primary . <b>NonWildTypeArguments</b>? Identifier ( ArgumentList? )<br /> super . NonWildTypeArgumentsopt Identifier ( ArgumentList? )<br /> ClassName . super . NonWildTypeArguments? Identifier ( ArgumentList? )<br /> TypeName . NonWildTypeArguments Identifier ( ArgumentList? )<br /></div><br /><br />And here's a concrete example:<br /><br /><div class="code">foo(Collections.<b><String></b>emptyList());<br /></div>Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com28tag:blogger.com,1999:blog-9469017.post-18868803652069636152007-02-25T16:05:00.000-08:002007-02-25T16:12:18.385-08:00rubydbc<a href="http://split-s.blogspot.com/2006/02/design-by-contract-for-ruby.html">Design by Contract for Ruby</a> is now a <a href="http://rubyforge.org/projects/rubydbc">Rubyforge project</a>. Version 1.0.0 is available as a gem:<br /><br /><span style="font-family: courier new;">gem install rubydbc</span>Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com23tag:blogger.com,1999:blog-9469017.post-1162952287276382832006-11-07T16:51:00.000-08:002006-11-07T18:19:36.863-08:00Extensible mod_rewrite with LuaI was running an experiment with apache + mod_rewrite that required rules that cannot be expressed with regular expressions. mod_rewrite has the notion of <span style="font-style:italic;">rewrite maps</span>, which let you choose from a couple of built in functions (int:tolower, int:toupper, etc.) or a key-value pair map backed by a text or dbm file. <br /><br />Unfortunately, there's no way to plug in arbitrary mapping functions<a href="#footnote">*</a>, so my first thought was to extend mod_rewrite by adding a new built in function. This lets me write:<br /><br /><div class="code"><br />LoadModule rewrite_module modules/mod_rewrite.so<br />RewriteEngine on<br />RewriteMap foo int:foo<br /><br />RewriteRule (.*) ${foo:$1} [L] <br /></div><br /><br />It works great, but this approach requires forking mod_rewrite.c, which is obviously not a good thing.<br /> <br />Inspired by <a href="http://kasparov.skife.org/blog/src/wombat/httpd-conf-cool.html">Brian's mod_wombat</a>, which allows you write http handlers in Lua, I thought "wouldn't it be nice to be able to extend mod_rewrite with custom Lua scripts?". Say, something like:<br /><br /><br /><div class="code"><br />LoadModule rewrite_module modules/mod_rewrite.so<br />RewriteEngine on<br />RewriteMap foo lua:/usr/local/apache2/rewrite-rules.lua<br /><br />RewriteRule (.*) ${foo:$1} [L] <br /></div><br /><br /><br />30 lines of code later, my lua-enabled mod_rewrite prototype is able to do exactly that. Mind you, this is a very crude implementation and doesn't check for errors, cache compiled scripts, etc. But all those details can be improved.<br /><br />Anyway, this is all becomes somewhat irrelevant with the latest version of mod_wombat, which allows you to define transform_name hooks in Lua, as well. <br /><br />It was a interesting experiment, nonetheless :)<br /><br /><br /><br /><a name="footnote">*</a> To be fair, there <span style="font-style:italic;">is</span> actually a way to do this, but I consider it to be too clunky and non-scalable to be of any value in a real production system. The mechanism consists on mod_rewrite talking to an external process that does translations via its stdin/stdout streams. As a result, a global mutex is required to guarantee correctness in the face of multiple simultaneous requests.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com4tag:blogger.com,1999:blog-9469017.post-1146374164274211472006-04-29T22:09:00.000-07:002006-04-29T22:22:31.453-07:00application/atom+xml content type workaround for FirefoxHere's a Firefox <a href="http://utils.ning.com/firefox">extension</a> that will let you view Atom 1.0 feeds inside your browser. Currently, Firefox does not understand the application/atom+xml content type and will treat the feed as a download and prompt you to save it or to open it with an external application. <br /><br />Once you install this extension, Atom feeds will be displayed in your browser like any other xml document.<br /><br />Get it <a href="http://utils.ning.com/firefox/atom-workaround-0.1.xpi">here</a>!Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com4tag:blogger.com,1999:blog-9469017.post-1145741859142670872006-04-22T14:20:00.000-07:002006-04-22T15:24:16.343-07:00Shoot ProfilesIf you are into <a href="http://clans.gameclubcentral.com/shoot">Shoot</a> and games, you may appreciate this <a href="http://shootprofiles.ning.com">application</a> for uploading and sharing Shoot profiles. <br /><br />I uploaded the profiles from the <a href="http://clans.gameclubcentral.com/shoot/profilelibrary.php">old site</a> and added a few that I hadn't got around to posting. Feel free to <a href="http://shootprofiles.ning.com/index.php/profiles/index/add">share</a> yours!Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com2tag:blogger.com,1999:blog-9469017.post-1144382844929928012006-04-06T19:41:00.000-07:002006-12-13T10:31:54.231-08:00Atom 1.0 parser for RubyI've been looking for a Ruby library for parsing <a href="http://www.ietf.org/rfc/rfc4287">Atom 1.0</a>. The only one I could find was <a href="http://www.rubyforge.org/projects/feedtools">FeedTools</a>. Unfortunately, it's intended to be a universal feed-parsing library and not quite what I need.<br /><br />As a result, I decided to build my own. The <a href="http://www.rubyforge.org/projects/atom">project</a> is hosted at RubyForge and the source code is available under the MIT license.<br /><br />It is still in its early stages, so don't expect much at this point. So far, it understands most atom:* elements and attributes but doesn't perform validation.<br /><br />The interface is fairly simple. For example, you can do this:<br /><br /><div class="code"><br />require 'atom'<br />require 'net/http'<br />require 'uri'<br /><br />str = Net::HTTP::get(URI::parse('http://blog.ning.com/atom.xml'))<br />feed = Atom::Feed.new(str)<br /><br />feed.entries.each { |entry|<br /> puts "'#{entry.title}' by #{entry.authors.first.name} on #{entry.published.strftime('%m/%d/%Y')}"<br />}<br /></div><br /><br />Give it a try, play with it. Comments are welcome!Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com14tag:blogger.com,1999:blog-9469017.post-1142469469355542392006-03-15T16:22:00.000-08:002006-12-13T10:32:24.780-08:00Where am I?Need to get the name of the current method in Ruby?<br /><br /><div class="code"><br />module Kernel<br /> def current_method<br /> caller[0].sub(/.*`([^']+)'/, '\1').to_sym<br /> end<br />end<br /></div><br /><br /><pre><br />irb(main):006:0> def test<br />irb(main):007:1> p current_method<br />irb(main):008:1> end<br />=> nil<br />irb(main):009:0> test<br />:test<br />=> nil<br />irb(main):010:0><br /></pre><br /><br />If you want to get fancier, you can get get a reference to the method itself:<br /><br /><div class="code"><br />module Kernel<br /> def current_method<br /> method(caller[0].sub(/.*`([^']+)'/, '\1').to_sym)<br /> end<br />end<br /></div>Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com0tag:blogger.com,1999:blog-9469017.post-1137833124728169072006-02-21T23:30:00.000-08:002006-02-21T23:35:12.953-08:00Design by contract for RubyHere's a nice little module <a href="http://kasparov.skife.org/blog/">Brian</a> and I hacked together a few weeks ago. It adds basic design by contract capabilities to classes with a fairly clean syntax (yay for Ruby's flexibility!)<br /><br />Take a look for yourself:<br /><br /><div class="code">class Foo<br /> include DesignByContract<br /><br /> pre(:sqrt, "value must be >= 0") { |v| v >= 0 }<br /><br /> pre("divisor must be != 0") { |dividend, divisor| divisor != 0 }<br /> post { |result, a, b| a - b * result < 1e-3 }<br /> def div(dividend, divisor)<br /> dividend / divisor<br /> end<br /> <br /> def sqrt(value)<br /> Math.sqrt value<br /> end<br /><br /> post(:sqrt, "error is greater than expected") { |result, value| <br /> result * result - value < 1e-3 <br /> }<br />end<br /></div><br /><br />When the DesignByContract module is included in a class, the <em>pre</em> and <em>post</em> methods become available. The parameters to these methods are a symbol representing the method to add the pre or post condition to, the message to print if the condition fails and a block that implements the condition.<br /><br />The method name and message are optional, though. If the method name is missing, the condition will applied to the first method definition following the pre/post declaration. Note that if the method name is present, the condition can be defined anywhere in the class definition, as shown in the example above. <br /><br />The condition block receives all the arguments passed to the method it applies to. They must return a boolean value indicating whether the condition succeeded. A minor difference between pre and post blocks is that the post block receives the result returned by the method as the first argument. This makes it possible to validate the results against the inputs such as in:<br /><br /><div class="code">post { |result, dividend, divisor| dividend - divisor * result < 1e-3 }</div><br /><br />Get it <a href="http://utils.ning.com/ruby/dbc.rb">here</a>!<br /><br /><h2>How it works</h2><br />In a nutshell, the pre and post methods intercept the existing method using the <a href="http://split-s.blogspot.com/2006/01/replacing-methods.html">technique</a> I described a few weeks ago (i.e., no poluting the class' namespace with aliased methods). The new methods test the provided condition before or after delegating the call to the original method and raise an exception if the condition fails.<br /><br />There is a small caveat, though. If pre/post are called <em>before</em> the method is first created, it cannot be intercepted. To get around this the module uses an alternative trick. Whenever the module is included, it hooks into the method_added callback of class it's included into. If pre/post are called and the corresponding method does not exist, the interception is scheduled until the method is added.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com12tag:blogger.com,1999:blog-9469017.post-1138177085499973272006-01-25T00:15:00.000-08:002006-02-01T23:23:02.366-08:00Duplicate Message Remover for Thunderbird 1.5I've updated the <a href="http://split-s.blogspot.com/2005/02/duplicate-message-remover-01.html">Duplicate Message Remover</a> extension to make it compatible with Thunderbird 1.5. Grab it <a href="http://utils.ning.com/thunderbird/">here</a>.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com5tag:blogger.com,1999:blog-9469017.post-1137830130599421622006-01-20T22:43:00.000-08:002006-01-21T00:12:54.890-08:00Replacing methodsLet's say we want to replace a method instead of just <a href="http://split-s.blogspot.com/2006/01/removing-methods-from-module.html">throwing it away</a>. But we want the new method to be able to call the old implementation.<br /><br />The standard way of doing this is by aliasing the old method and making the new one call the aliased version:<br /><br /><div class="code"><br />class Foo<br /> def method<br /> ...<br /> end<br /><br />...<br /><br />class Foo<br /> alias :method :old_method<br /><br /> def method<br /> ...<br /> old_method<br /> end<br />end<br /><br /></div><br /><br />This solution works perfectly for most situations, but what if we want to remove all traces of the old method?<br /><br />Here's a simple trick to do exactly that:<br /><br /><div class="code"><br />class Foo<br /> old_method = instance_method(:method)<br /><br /> define_method(:method) { |*args|<br /> # do something here ...<br /> old_method.bind(self).call(*args)<br /> }<br />end<br /></div><br /><br />It shouldn't take much to understand how it works, but let's analyze it in more detail. The first line gets a hold of the old implementation of <span style="font-family: Courier New, Monospaced">method</span>. <span style="font-family: Courier New, Monospaced">instance_method</span> returns an UnboundMethod object that wraps it.<br /><br />Next, we redefine <span style="font-family: Courier New, Monospaced">method</span> and pass it a block with the new implementation. One thing worth mentioning is that the <span style="font-family: Courier New, Monospaced">old_method</span> variable inside the block is bound to the variable assigned to outside of the block. The binding is maintained even after the execution reaches the end of the class definition. As a result, the new <span style="font-family: Courier New, Monospaced">method</span> has a direct reference to the old one... and nobody else does.<br /><br />After doing whatever it has to do, the new method calls the old one after binding the UnboundMethod to the current object (i.e., the object on which <span style="font-family: Courier New, Monospaced">method</span> was invoked). <br /><br />If you have a keen eye, you'll notice that there's something special about <span style="font-family: Courier New, Monospaced">self</span>. At first glance, it looks like a standard variable that just happens to hold a reference to the current object. If that were true, it would follow that the reference to <span style="font-family: Courier New, Monospaced">self</span> inside the block would be bound to the <span style="font-family: Courier New, Monospaced">self</span> representing class Foo. That is not the case, though. The binding of <span style="font-family: Courier New, Monospaced">self</span> is established when the block gets called.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com3tag:blogger.com,1999:blog-9469017.post-1137560897365377172006-01-17T20:59:00.000-08:002006-01-18T00:07:58.820-08:00Removing methods from a moduleHow do we remove a method from one of Ruby's built-in modules (Kernel, etc)? It should be easy and obvious, right? The following snippet should be enough, right?<br /><br /><div class="code"><br />#remove system from module Kernel<br />Kernel.send :remove_method, :system <br /></div><br /><br />Well, not quite. While that prevents you from calling system() in the context of the current object, you can still invoke it like this:<br /><br /><div class="code"><br />Kernel::system('date')<br /></div><br /><br />What's going on? <br /><br />It turns out that they way Kernel (and other built-in modules) are defined is analogous to doing the following:<br /><br /><div class="code"><br />module Kernel<br /> def system (...)<br /> ...<br /> end<br /><br /> module_function :system<br />end<br /></div><br /><br />Thus, system() is a method that will be added to instances of classes that <em>include</em> the given module (in our case, instances of any object, since Kernel is included by Object), but it is also a method in Kernel's singleton class (bear with me for a moment), or a <em>module_method</em> as the PickAxe book calls them. In Java parlance, they are simply static methods.<br /><br />Looking at Ruby's source code, we can see that all of Kernel's methods are defined by way of the <em>rb_define_module_function</em> function, whose definition is:<br /><br /><div class="code"><br />void<br />rb_define_module_function(module, name, func, argc)<br /> VALUE module;<br /> const char *name;<br /> VALUE (*func)();<br /> int argc;<br />{<br /> rb_define_private_method(module, name, func, argc);<br /> rb_define_singleton_method(module, name, func, argc);<br />}<br /></div><br /><br />So, what does it mean that "system" is a method of Kernel's singleton class? Let's recap what happens when we create a class and add a method to the singleton class of one of it's instances.<br /><br /><div class="code"><br />class A<br />end<br /><br />V1 = A.new # creates an instance of A and assign it to constant V1<br />V2 = A.new # ... and V2<br /><br /># now we add method x() to V1's singleton class<br />class << V1<br /> def x<br /> puts "hello"<br /> end<br />end<br /><br />V1.x # will print "hello"<br />V2.x # will fail ... V2 does not have a method x() <br /></div><br /><br />Clear?<br /><br />Ok, now, remember that all Ruby modules are instances of a class called Module? Say we replace A by Module, V1 by Kernel and x() by system(). What we get is the following:<br /><br /><div class="code"><br />Kernel = Module.new<br /><br />class << Kernel<br /> def system<br /> ...<br /> end<br />end<br /></div><br /><br />And that's essentially what's going on behind the scenes.<br /><br />Going back to the question that started this post, how do we remove a method from one of the built-in modules, anyway? The answer is easy, if not as obvious as we might have expected:<br /><br /><div class="code"><br />module Kernel<br /> remove_method :system<br />end<br /><br />class << Kernel<br /> remove_method :system<br />end<br /></div><br /><br />Or, more concisely,<br /><br /><div class="code"><br />Kernel.send :remove_method, :system <br />class << Kernel; self; end.send :remove_method, :system<br /></div>Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com2tag:blogger.com,1999:blog-9469017.post-1136933063431005712006-01-10T14:35:00.000-08:002006-01-10T14:44:23.486-08:00Flying dogLOL!<br /><br /><a href="http://video.google.com/videoplay?docid=2978884470072026171&q=flying+dog">http://video.google.com/videoplay?docid=2978884470072026171&q=flying+dog</a>Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com0tag:blogger.com,1999:blog-9469017.post-1135297933422039442005-12-22T16:19:00.000-08:002005-12-22T22:21:36.380-08:00Ruby's executable class definitionsIf you're coming to Ruby from Java, remember that class definitions are <a href="http://split-s.blogspot.com/2005/02/definition-time-execution-time-anytime.html">executable</a> code. I can think of at least one situation where this has practical implications and if you're not clear about it you'll spend a couple of hours banging your head against the wall.<br /><br />Consider this simple Java class:<br /><div class="code"><br />class A<br />{<br /> int a = 10;<br /><br /> public int getA()<br /> {<br /> return a;<br /> }<br />}<br /></div><br /><br />If you create an object of type A and call it's method getA(), you'll get 10. No surprises here.<br /><br />Now, knowing that member variables in Ruby are prefixed with @ we go ahead and translate our little java class to Ruby like so:<br /><br /><div class="code"><br />class A<br /> @a = 10<br /><br /> def a<br /> return @a<br /> end<br />end<br /></div><br /><br />We then create an object of class A and call its a() method:<br /><pre><br />irb(main):001:0> class A<br />irb(main):002:1> @a = 10<br />irb(main):003:1> def a<br />irb(main):004:2> return @a<br />irb(main):005:2> end<br />irb(main):006:1> end<br />=> nil<br />irb(main):007:0> A.new.a<br />=> nil<br /></pre><br /><br />"WTF!!" you say. Well, if you remembered what I said, class definitions are executable code. In our case, this means that the @a = 10 is executed when the class is defined, not when an instance of the class is created. As a result, this statement defines a member variable of the instance of class <em>Class</em> that represents class A.<br /><br />To prove the last statment is true, let's make a() a class method:<br /><div class="code"><br />class A<br /> @a = 10<br /><br /> def A.b<br /> return @a<br /> end<br />end<br /></div><br /><br />When we call it we get what we expected:<br /><pre><br />irb(main):001:0> class A<br />irb(main):002:1> @a = 10<br />irb(main):003:1> def A.a<br />irb(main):004:2> return @a<br />irb(main):005:2> end<br />irb(main):006:1> end<br />=> nil<br />irb(main):007:0> A.a<br />=> 10<br /></pre>Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com0tag:blogger.com,1999:blog-9469017.post-1135270058337517012005-12-22T08:32:00.000-08:002005-12-22T14:11:07.340-08:00Simple color effects<a href="http://en.wikipedia.org/wiki/RGB">RGB</a> is the most widespread method for representing colors. HTML, CSS, Java's java.awt.Color constructors, and countless more, all expect color to be described in terms of their RGB components. <br /><br />But RGB is not the best choice for doing common operations such as desaturation and certain types of gradients (e.g., shades of a color and fade to black or white). There's a much more natural way of manipulating colors for that purpose without going crazy with interpolation in the RGB color space. The trick is to work with the <a href="http://en.wikipedia.org/wiki/HSV_color_space">HSV</a> representation, instead. In the HSV space, a color is defined by it's hue (the "rainbow" of colors), it's saturation and it's brightness or value.<br /><br />For any given color, change the brightness to get a darker or lighter shade. If you want to desaturate it (without changing it's brightness), simply reduce it's saturation value.<br /><br />Here's a simple <a href="http://clans.gameclubcentral.com/shoot/color.html">example</a>, and the relevant Javascript code:<br /><br /><div class="code"><br />function RGB(r, g, b) <br />{<br /> this.r = r<br /> this.g = g<br /> this.b = b<br />}<br /><br />RGB.prototype = {<br /> toHSV: function()<br /> {<br /> var max = Math.max(this.r, this.g, this.b)<br /> var min = Math.min(this.r, this.g, this.b)<br /><br /> var s = (max - min) / max<br /><br /> switch (max) {<br /> case this.r: return new HSV(60 * (this.g - this.b) / (max - min), s, max)<br /> case this.g: return new HSV(60 * (this.b - this.r) / (max - min) + 120, s, max)<br /> case this.b: return new HSV(60 * (this.r - this.g) / (max - min) + 240, s, max)<br /> }<br /> },<br /><br /> toHex: function()<br /> {<br /> var r = Math.floor(this.r * 255).toString(16)<br /> var g = Math.floor(this.g * 255).toString(16)<br /> var b = Math.floor(this.b * 255).toString(16)<br /><br /> return "#" + (r.length == 1? "0" : "") + r<br /> + (g.length == 1? "0" : "") + g<br /> + (b.length == 1? "0" : "") + b<br /> }<br />}<br /><br /><br />function HSV(h, s, v)<br />{<br /> this.h = h<br /> this.s = s<br /> this.v = v<br />}<br /><br />HSV.prototype = {<br /> toRGB: function()<br /> {<br /> var sextant = Math.floor(this.h / 60) % 6<br /><br /> var f = this.h / 60 - sextant<br /> var p = this.v * (1 - this.s)<br /> q = this.v * (1 - f * this.s)<br /> t = this.v * (1 - (1 - f) * this.s)<br /><br /> switch (sextant) {<br /> case 0: return new RGB(this.v, t, p)<br /> case 1: return new RGB(q, this.v, p)<br /> case 2: return new RGB(p, this.v, t)<br /> case 3: return new RGB(p, q, this.v)<br /> case 4: return new RGB(t, p, this.v)<br /> case 5: return new RGB(this.v, p, q)<br /> }<br /> },<br /><br /> toHex: function()<br /> {<br /> return this.toRGB().toHex()<br /> }<br />}<br /><br /><br />function desaturate(box, color)<br />{<br /> box.style.backgroundColor = color.toHex()<br /><br /> color.s -= .01<br /> if (color.s >= 0) {<br /> window.setTimeout(function() { desaturate(box, color), 50 })<br /> }<br />}<br /><br />function fadeToBlack(box, color)<br />{<br /> box.style.backgroundColor = color.toHex()<br /><br /> color.v -= .01<br /> if (color.v >= 0) {<br /> window.setTimeout(function() { fadeToBlack(box, color), 50 })<br /> }<br />}<br /><br />function fadeToWhite(box, color)<br />{<br /> box.style.backgroundColor = color.toHex()<br /><br /> var v = 1 - color.v<br /> var distance = Math.sqrt(color.s * color.s + v * v)<br /><br /> color.s -= 0.01 * color.s / distance<br /> color.v += 0.01 * v / distance<br /><br /> if (color.v <= 1 && color.s >= 0) {<br /> window.setTimeout(function() { fadeToWhite(box, color), 50 })<br /> }<br />}<br /><br /></div>Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com1tag:blogger.com,1999:blog-9469017.post-1135211426210161192005-12-21T16:22:00.000-08:002005-12-21T17:44:55.736-08:00Internationalized GET parameters with TomcatOne tiny, oft overlooked detail when working with internationalized web apps is that the Content-Type header specifies the type and encoding of the <span style="font-weight:bold;">body</span> of the http request. <br /><br />Say you want to add searching to your site, which is implemented as a form that submits using method="GET", and your target audience is Japanese people. How do you deal with internationalized query parameters, considering that GET requests do not have a body? <br /><br />Historically, query strings were expected to contain characters in the ISO-8859-1 character set, with anything but a subset of ASCII encoded using the %xx notation. In discussions a few years ago it was agreed that using UTF-8 encoding for international characters was the best alternative. This is reflected in <a href="http://www.gbiv.com/protocols/uri/rfc/rfc3986.html">RFC 3986</a> (which is backed by the <a href="http://www.w3.org/International/O-URL-and-ident.html">W3C</a>):<br /><br /><blockquote><em><br />When a new URI scheme defines a component that represents textual data consisting of characters from the Universal Character Set [UCS], the data should first be encoded as octets according to the UTF-8 character encoding [STD63]; then only those octets that do not correspond to characters in the unreserved set should be percent-encoded. For example, the character A would be represented as "A", the character LATIN CAPITAL LETTER A WITH GRAVE would be represented as "%C3%80", and the character KATAKANA LETTER A would be represented as "%E3%82%A2".</em><br /></blockquote><br /><br />If you're running Tomcat, watch out! If your URL contains unicode characters, the browser will properly encode them as UTF-8 and then do the %xx magic for each byte. Tomcat will treat each encoded byte in the UTF-8 representation as a single ISO-8859-1 character and make your Japanese users very unhappy.<br /><br />Unfortunately, this is Tomcat's default behavior. Fortunately, there is server configuration option that will force it to treat URLs as per RFC 3986. To enable it, edit server.xml and add the following parameter to the <span style="font-style:italic;">connector</span> element:<br /><br /><div class="code"><br /><Server ...><br /> <Service ...><br /> <Connector ... <span style="font-weight:bold;">URIEncoding="UTF-8"</span>/><br /> ...<br /> </Connector><br /> </Service><br /></Server><br /></div><br /><br />BTW, I've tested this in Tomcat 5.5.12. I don't know whether it will work in previous versions.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com7tag:blogger.com,1999:blog-9469017.post-1134707761562617692005-12-15T20:28:00.000-08:002005-12-15T21:04:39.483-08:00ANTLR for RubyThe latest release of <a href="http://www.antlr.org/download/antlr-3.0ea7.tar.gz">ANTLR v3</a> adds support for Ruby as a target language, courtesy of yours truly. Let's look at an example to see what's possible.<br /><br />The following grammar generates a parser that can evaluate simple arithmetic expressions involving integer numbers, +, -, *, /, ( and ).<br /><br /><div class="code"><br />grammar Calculator;<br />options {<br /> language = Ruby;<br />}<br /><br />@members {<br /> @stack = []<br /><br /> def result<br /> @stack[0]<br /> end<br />}<br /><br />parse: expression;<br /><br />expression: mult (<br /> '+' mult {<br /> @stack.push(@stack.pop + @stack.pop)<br /> }<br /> | '-' mult {<br /> a = @stack.pop<br /> b = @stack.pop<br /> @stack.push(b - a)<br /> }<br /> )* ;<br /><br />mult: atom (<br /> '*' atom {<br /> @stack.push(@stack.pop * @stack.pop)<br /> }<br /> | '/' atom {<br /> a = @stack.pop<br /> b = @stack.pop<br /> @stack.push(b / a.to_f)<br /> }<br /> )* ;<br /><br /><br />atom: n=NUMBER { @stack.push($n.text.to_i) }<br /> | '(' expression ')';<br /><br />NUMBER: ('0'..'9')+;<br /><br />WS: (' ' | '\n' | '\t')+ { channel = 99 };<br /></div><br /><br />First, we need to process the grammar with ANTLR. You'll need to have Java installed in order to do this. ANTLR will generate two files: Calculator.rb and CalculatorLexer.rb.<br /><br />You'll also need the ANTLR ruby runtime library (just a small .rb file) which you can get <a href="http://clans.gameclubcentral.com/shoot/antlr.rb">here</a>. Just drop into the same directory as the two .rb files created above (in the future I may package it as a gem).<br /><br />We got our parser, so let's try it out. Create another ruby file with the following:<br /><br /><div class="code"><br />require 'Calculator'<br />require 'CalculatorLexer'<br /><br />lexer = CalculatorLexer.new(ANTLR::CharStream.new(STDIN))<br />parser = Calculator.new(ANTLR::TokenStream.new(lexer))<br />parser.parse<br /><br />puts parser.result<br /></div><br /><br />Now, run the program, type an expression and press CTRL-D (or CTRL-Z if in Windows) to see the results, like so:<br /><br /><pre><br />(5 + 4) * (10 - 2) / 3<br />^Z<br />24.0<br /></pre><br /><br />Simple... but cool, eh?<br /><br />ANTLR v3 is still in pre-alpha stage, and the Ruby backend is a work in progress. So far, it can handle syntactic and semantic predicates, token labels, etc. and I'm planning to add support for scopes and token/rule parameters and return values next. AST creation will come after that.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com17tag:blogger.com,1999:blog-9469017.post-1133000398482121292005-11-26T01:03:00.000-08:002005-11-26T02:21:36.006-08:00ANTLR-based Ruby grammarIt looks like <a href="http://moonbase.rydia.net/mental/blog/programming/the-ruby-grammar-project.html">other people</a> are showing interest in replacing the YACC-based Ruby parser by one based on ANTLR. This hits close to a project I've been working on in my spare time.<br /><br />When I looked at JRuby's source code a few weeks ago, I thought I'd start trying to understand how the parser works. I had a few ideas I wanted to experiment with, but... <br /><br />The YACC-based parser is too hard to fathom. There are too many scattered pieces with no clear dependencies. Making changes without breaking things should be even harder. <br /><br />So I decided to start working on ANTLR-based grammar to replace the current implementation -- pretty much what <a href="http://moonbase.rydia.net/">MenTaLguY</a> is trying to do. I'm still in experimentation mode and trying to figure out how I'll deal with some of the features of Ruby that are not usually found in other languages.<br /><br />One of the hardest aspects, I think, is how to parse %Q-delimited strings. Here's how they work (from the Pickaxe book): <br /><br /><blockquote><br />Following the type character is a delimiter, which can be any nonalphabetic or nonmultibyte<br />character. If the delimiter is one of the characters (, [, {, or <, the literal<br />consists of the characters up to the matching closing delimiter, taking account of nested<br />delimiter pairs. For all other delimiters, the literal comprises the characters up to the<br />next occurrence of the delimiter character.<br /></blockquote><br /><br />Additionally, %Q strings can contain #{} constructs, whose contents should be parsed as Ruby expressions. These can probably be handled in a manner similar to how this grammar for the <a href="http://www.erights.org/e-impls/e-on-e/egrammar/">E language</a> implements what they call <em>quasi-literals</em>. <br /><br />I haven't yet decided on how to deal with delimiters but I have the feeling it may require writing a custom lexer for string contexts.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com3tag:blogger.com,1999:blog-9469017.post-1128813198325460502005-10-08T16:06:00.000-07:002005-10-08T16:13:25.473-07:00Firefox search plugins for NingIf you're using Firefox, you can get a few search plugins for Ning here:<br /><br /><a href="http://utils.ning.com/plugin/">http://utils.ning.com/plugin/</a>Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com2tag:blogger.com,1999:blog-9469017.post-1128533793418399552005-10-05T10:07:00.000-07:002005-10-05T10:36:33.426-07:00It's aliiiive!A couple of days ago we launched <a href="http://www.ning.com">Ning</a>, a playground for building social applications. Lots of work in the past few months -- that's my lame excuse for not having posted this long :) --, working <a href="http://www.sklar.com/blog">with</a> a <a href="http://www.dynamicobjects.com/d2r">great</a> <a href="http://www.bitsplitter.net/blog">team</a> (and others that don't have a blog to link to).<br /><br />The next few months are going to be exciting!Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com1tag:blogger.com,1999:blog-9469017.post-1111791463331717312005-03-25T14:44:00.000-08:002005-03-25T14:57:43.333-08:00Goodbye Texas, hello Californiasplit-s has been silent lately. A lot has been happening in recent weeks, and my mind has been elsewhere.<br /><br />I'm leaving my current job in Plano and moving to the Bay Area. I can't say what I'll be doing, yet. <a href="http://www.dynamicobjects.com/d2r/archives/003072.html">Stealth-mode and all that</a> :). All I'll say for now is that I'll be working with <a href="http://www.dynamicobjects.com/d2r">Diego</a> and a few other great people.<br /><br />Now, to start planning for the move. It's gonna be very hectic for the next few weeks, but I'm excited!Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com1tag:blogger.com,1999:blog-9469017.post-1110302284550838112005-03-08T08:58:00.000-08:002005-03-08T09:25:54.693-08:00const_missingThere's an ongoing weekly quiz in the Ruby mailing list. This week's theme is roman numerals -- writing a program that can read roman numerals from the standard input and print out their arabic representation. A few solutions have been posted, standard stuff. But Dave Burt <a href="http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/133160">complemented</a> his with a very neat hack. He makes it possible to treat integers and roman numerals interchangeably, and to perform arithmetic operations with them:<br /><br />VII + 3 -> X<br />XIV / 2 -> VII<br />VIII.divmod(III) -> [II, II]<br />etc.<br /><br />Part of this is achieved by redefining the const_missing method of the Object class, which kicks in whenever the interpreter finds reference to an unknown constant.<br /><br /><div class="code">def Object.const_missing sym<br /> # verify that the constant is a roman numeral<br /> raise NameError.new("uninitialized constant: #{sym}") unless RomanNumerals::REGEXP === sym.to_s<br /> # define the constant and return it<br /> const_set(sym, RomanNumeral.get(sym))<br />end<br /></div><br /><br />The arithmetic operations are made possible by redefining RomanNumeral's method_missing method with code that delegates unknown operations to the it's integer representation. <br /><br />Take a <a href="http://www.dave.burt.id.au/ruby/roman_numerals.rb">peek</a> at the complete solution.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com0tag:blogger.com,1999:blog-9469017.post-1109787723014265182005-03-02T10:21:00.000-08:002005-03-02T10:27:13.356-08:00Stealth fighter jet unveiled<a href="http://www.zeeb.at/oops/show.php?file=Stealth fighter.jpg">See</a> for yourself...<br /><br /><br />I had a good laugh with these, too. Enjoy!<br /><br /><a href="http://www.zeeb.at/oops/show.php?file=DamnGoodTailHook.jpg">1</a> <a href="http://www.zeeb.at/oops/show.php?file=Fill-er-up.jpg">2</a> <a href="http://www.zeeb.at/oops/show.php?file=ForecastingStone.jpg">3</a> <a href="http://www.zeeb.at/oops/show.php?file=Nothing_Changes.jpg">4</a> <a href="http://www.zeeb.at/oops/show.php?file=SpeedEnforcement1.jpg">5</a> <a href="http://www.zeeb.at/oops/show.php?file=SpeedEnforcement2.jpg">6</a> <a href="http://www.zeeb.at/oops/show.php?file=WindowsPanel.jpg">7</a> <a href="http://www.zeeb.at/oops/show.php?file=wyoming_windsock.jpg">8</a>Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com2tag:blogger.com,1999:blog-9469017.post-1109265078178018242005-02-24T09:05:00.000-08:002005-02-24T09:36:25.943-08:00Ruby-style iterators in JavaLoop abstraction is probably the most common application of <a href="http://split-s.blogspot.com/2005/02/building-blocks.html">blocks</a> in Ruby. The idea being that the programmer should not have to deal with the iteration code itself and concentrate on what needs to be done at each step, instead. And it's typically a good idea.<br /><br />This is what a standard pre-Java 5 iteration looks like:<br /><br /><div class="code">Iterator i = collection.iterator();<br />while (i.hasNext()) {<br /> Object obj = i.next();<br /> ...<br />}<br /></div><br />Or, for iterating over an array:<br /><br /><div class="code">for (int i = 0; i < array.length; ++i) {<br /> Object obj = array[i];<br /> ...<br />}<br /></div><br />These are cleary prone to cause programming errors. Forget to call i.next(), call it twice, use <= instead of <, forget ++, and all goes to hell.<br /><br />Iterators have gotten significantly better in Java 5 with the introduction of the <em>for each</em> construct, but they are still a long way from Ruby's internal iterators:<br /><br /><div class="code">for (String str : listOfStrings) {<br /> ...<br />}<br /></div><br />A little bit of nomenclature here. External iterators are those where the client code controls how the iteration takes place (e.g., the Java way). Internal iterators, on the other hand, abstract that logic and only require the client code to supply the action to perform at each step of the iteration (e.g., the Ruby way). <br /><br />Take a look at the iterator idiom in Ruby:<br /><br /><div class="code">array.each { |value|<br /> ...<br />}<br /></div><br />Much cleaner, and no need for so much boilerplate, don't you think?<br /><br />In the majority of cases, what a program really needs are internal iterators. From a code reuse perspective, why replicate the same construct over and over? But make no mistake, there are some legitimate cases for where external iterators are necessary, such as when looping over several collections in parallel (try doing that with internal iterators).<br /><br />Ruby provides a way for <em>externalizing</em> internal iterators. Using the Generator class, an Enumerable object (i.e., that which exposes a method <em>each</em>) can be transformed into an external iterator:<br /><br /><div class="code">generator = Generator.new [1,2,3,4,5,6,7]<br /><br />while generator.next?<br /> puts generator.next<br />end<br /></div><br /><br /><b>Internal iterators in Java</b><br /><br />So, how about internal iterators for Java? The first thing we need to look at is how to encapsulate a chunk of code in a form that can be passed around (a la Ruby blocks).<br /><br />The closest we can get are anonymous inner classes, but it's not even fair to compare them with Ruby blocks. The latter a lot more flexible, without the clunky syntax. Here's an how we could emulate a general-purpose block in Java 5:<br /> <br /><div class="code">interface Block {<br /> Object call(Object... items);<br />}<br /><br />...<br /><br />void methodThatTakesBlock(Block block)<br />{<br /> block.call("hello", "world");<br />}<br /><br />...<br /><br />Block block = new Block() {<br /> Object call(Object... items) {<br /> return items[0].toString() + items[1].toString();<br /> }<br />}<br /><br />methodThatTakesBlock(block);<br /></div><br />First, you'll notice we're declaring a Block interface. This is needed in order to be able to invoke the "call" method on the block (it would be possible to do via reflection, too, but that's another story).<br /><br />The use of varargs helps avoid having to declare a block class or method for each possible combination of arguments and return values, but places the burden of extracting the arguments from the array on the implementer of the block.<br /><br />For what we're trying to achieve here, we're going to stick with non-vararg arguments. In fact, our block only needs one argument, namely, the element that's being iterated over at each step. Here it goes:<br /><br /><div class="code">interface IterationCallback<E><br />{<br /> void call(E element);<br />}<br /><br />interface EnumerableCollection<E><br /> extends Collection<E><br />{<br /> void each(IterationCallback<E> callback);<br />}<br /><br />class EnumerableArrayList<E><br /> extends ArrayList<E><br /> implements EnumerableCollection<E><br />{<br /><br /> public void each(IterationCallback<E> callback)<br /> {<br /> for (E element : this) {<br /> callback.call(element);<br /> }<br /> }<br />}<br /></div><br />And here's how to use it:<br /><br /><div class="code">EnumerableCollection<Integer> list = new EnumerableArrayList<Integer>();<br /><br /> list.add(1);<br /> list.add(2);<br /> list.add(3);<br /> list.add(4);<br /> list.add(5);<br /><br /> list.each(new IterationCallback<Integer>() {<br /> public void call(Integer element) {<br /> System.out.println(element);<br /> }<br /> });<br /></div><br />So, it is possible, but nobody said it would be pretty. Better language support would be needed to make internal iterators as developer-friendly as they are in Ruby. And we're not even considering the performance implications. The above code instantiates an extra object compared to using Java's iterator idiom, and requires an extra method call. It could have significant impact if used in the wrong place.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com3tag:blogger.com,1999:blog-9469017.post-1108570071068585922005-02-20T13:40:00.000-08:002005-02-20T13:58:29.733-08:00Do-not-call registry loophole?I received a call last Friday at home. I was at work, but the answering maching picked it up. The message said:<br /><blockquote>... This is not a sales call or a solicitation. Unfortunately, due to the nature of this matter, I am not able to provide additional information in this format. I, or one of my associates, can be reached at 1-800-XXX-XXXX. Please return this call..."<br /></blockquote>I used to get marketing calls every day, even Saturdays at 8 am. Really annoying. When I registered my phone number with the FCC do-not-call list over a year ago, the calls stopped. The rule does not limit some kinds of calls, though. To quote from the <a href="http://www.fcc.gov/cgb/donotcall/">FCC</a> site:<br /><ul><li>calls from organizations with which you have established a business relationship<br /><li>calls for which you have given prior written consent<br /><li>calls which are not commercial or do not include unsolicited advertisements<br /><li>calls by or on behalf of tax-exempt non-profit organizations<br /></ul><br />It seems to me that by saying "this is not a sales call or solicitation" they are attempting to cover themselves (see the 3rd item above). And if I call them back, then it would be me who's contacting them, so they would be exempted from the rule and free to try to sell me anything.<br /><br />I'm not calling them back, of course. But I hope this is not the beginning of a trend, or I may have to start shutting off my phone every Friday night... again.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com0tag:blogger.com,1999:blog-9469017.post-1108869975231975492005-02-19T17:47:00.000-08:002005-02-20T11:41:45.343-08:00building blocksOne of the features I like best in Ruby is the support for <em>blocks</em>. Blocks are, essentially, chunks of code that can be passed around as objects and invoked at a later point in the program (also known as <em>lambdas</em> in other languages). But they are more than just code. When a block is defined, the context in which it is defined (variables, constants, etc) goes with it.<br /><br />You define a block like this:<br /><br /><div class="code">b = lambda { |value| puts value }<br /><br />c = lambda do |value|<br /> puts value<br /> end<br /></div><br />Either {} or do...end can be used as delimiters. The names between | are the arguments to the block. Any number of arguments can be specified by separating them with comma: |value, i, j|<br /><br />To call the block:<br /><br /><div class="code">b.call("hello")<br /></div><br />The need for <em>call</em> <a href="http://redhanded.hobix.com/inspect/functionCall.html">may go away</a>, though.<br /><br />A little syntactic sugar allows us to pass blocks to methods implicitly. As far as I know, this syntax originated as a way to abstract looping behavior, one of the key applications of blocks in Ruby.<br /><br />This is how it works:<br /><br /><div class="code"> methodThatTakesBlock { |value| puts value }<br /></div><br />Sweet, eh?<br /><br />A method that takes a block can refer to it implicitly or explicitly. Implicitly:<br /><br /><div class="code">def methodThatTakesBlock <br /> yield "hello"<br />end<br /></div><br />The key here is the <em>yield</em> keyword, which calls the provided block with one or more values as arguments. <em>yield</em> can be called any number of times, for example:<br /><br /><div class="code">def methodThatTakesBlock <br /> yield "hello"<br /> yield "world"<br />end<br /></div><br />But what if you need to keep a reference to the block, or pass the block around from within that method? Easy. If the last parameter in a method declaration is prefixed with &, the provided block will be assigned to the parameter. Take a look for yourself:<br /><br /><div class="code">class BlockContainer<br /> def initialize<br /> @blocks = {}<br /> end<br /><br /> def addBlock(name, &block)<br /> @blocks[name] = block<br /> end<br />end<br /><br />container = BlockContainer.new<br />container.addBlock("a") { puts "a" }<br />container.addBlock("b") { puts "b" }<br /></div><br />Going back to what I said about blocks taking the context they were defined in with them... it is true! I swear! <br /><br />Ok, to illustrate the idea, look a this code:<br /><br /><div class="code">def methodThatTakesBlock<br /> yield<br />end<br /><br />a = 1<br />methodThatTakesBlock { puts a }<br /></div><br />Your first reaction might be "that won't work, <em>a</em> will not be available from within <em>methodThatTakesBlock</em>". In fact, it will, simply because <em>a</em> is part of the context where the block is defined.Martinhttp://www.blogger.com/profile/08718317252176743227noreply@blogger.com0