<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Posts about Ruby</title>
    <link>http://wiki.futuretoby.com/tag/ruby</link>
    <language>en-us</language>
    <item>
      <title>String Difference in Ruby</title>
      <description>&lt;pre&gt;class String&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;  def -(other)
    self.index(other) == 0 ? self[other.size..self.size] : nil
  end
end&lt;br&gt;&lt;/pre&gt;&lt;div&gt;This snippet gives the Ruby &lt;i&gt;String&lt;/i&gt; class a minus operator. Which works like&lt;/div&gt;&lt;pre&gt;&amp;gt; 'abcde' - 'abc'&lt;/pre&gt;&lt;pre&gt;=&amp;gt; "de"&lt;/pre&gt;&lt;pre&gt;&amp;gt; '    ' - '  '
=&amp;gt; "  "
&lt;/pre&gt;&lt;pre&gt;&amp;gt; 'abc' - 'de'
=&amp;gt; nil
&lt;/pre&gt;&lt;div&gt;Meh.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;</description>
      <pubDate>Fri, 25 Mar 2011 23:28:53 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/String_Difference_in_Ruby</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/String_Difference_in_Ruby</link>
    </item>
    <item>
      <title>HTTP Server in 5 Lines With Webrick</title>
      <description>Usually when I am prototyping a web UI - either in Javascript or Flex, I would just write a static html, because that's the simplest thing that works. But, once in a while, it doesn't work because of the security restrictions that the browser imposes on local files. Maybe you want to use ajax calls(which is sometimes problematic on IE), trying to use the google maps api, or the FABridge, whatever the reason may be. Well, you can get around this problem easily using this ruby script:&lt;pre&gt;require 'webrick'
server = WEBrick::HTTPServer.new :Port =&amp;gt; 1234
server.mount "/", WEBrick::HTTPServlet::FileHandler, './'
trap('INT') { server.stop }
server.start&lt;br&gt;&lt;/pre&gt;&lt;div&gt;This runs a web server at&amp;nbsp;&lt;a href="http://localhost:1234/"&gt;http://localhost:1234/&lt;/a&gt;&amp;nbsp;which mounts the top level directory to your current directory.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;Update: Oops, it's not exactly that easy after all. In order to prevent caching - which you will want to do if you are doing development - you will want to write an extra class. The modified script:&lt;/i&gt;&lt;/div&gt;&lt;pre&gt;require 'webrick'
class NonCachingFileHandler &amp;lt; WEBrick::HTTPServlet::FileHandler
  def prevent_caching(res)
    res['ETag']          = nil
    res['Last-Modified'] = Time.now + 100**4
    res['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
    res['Pragma']        = 'no-cache'
    res['Expires']       = Time.now - 100**4
  end
  
  def do_GET(req, res)
    super
    prevent_caching(res)
  end

end

server = WEBrick::HTTPServer.new :Port =&amp;gt; 1234
server.mount "/", NonCachingFileHandler , './'
trap('INT') { server.stop }
server.start&lt;br&gt;&lt;/pre&gt;&lt;div&gt;&lt;i&gt;Not 5 lines anymore, bummer! The code for NonCachingFileHandler was stolen &amp;nbsp;from unittest_js.&lt;/i&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 16 Sep 2009 10:58:35 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/HTTP_Server_in_5_Lines_With_Webrick</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/HTTP_Server_in_5_Lines_With_Webrick</link>
    </item>
    <item>
      <title>The New-Found-Love for Dynamic Languages</title>
      <description>&lt;a href="http://stackoverflow.com/questions/42934/whats-with-the-love-of-dynamic-languages/1228619#1228619"&gt;My response to a SO question.&lt;/a&gt;&lt;br&gt;</description>
      <pubDate>Tue, 04 Aug 2009 12:57:20 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/The_New-Found-Love_for_Dynamic_Languages</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/The_New-Found-Love_for_Dynamic_Languages</link>
    </item>
    <item>
      <title>Null in 10 Languages</title>
      <description>&lt;ul&gt;&lt;li&gt;Java, Javascript, C#: null&lt;/li&gt;&lt;li&gt;Ruby, Smalltalk, Lisp, OCaml: nil&lt;/li&gt;&lt;li&gt;Lisp: ()&lt;/li&gt;&lt;li&gt;Python: None&lt;/li&gt;&lt;li&gt;Haskell: Nothing&lt;/li&gt;&lt;li&gt;C: 0&lt;/li&gt;&lt;/ul&gt;</description>
      <pubDate>Mon, 11 May 2009 13:39:21 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Null_in_10_Languages</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Null_in_10_Languages</link>
    </item>
    <item>
      <title>ActiveRecord Gotcha</title>
      <description>I came across this interesting gotcha while debugging through someone else's rails code. When you set a model's one-to-many attribute(an perhaps other association types as well), let's say:&lt;pre&gt;user.friends = new_friend_list&lt;/pre&gt;&lt;div&gt;It doesn't always set it. As far as I can figure from my blackbox testing, it does an equal check first between the old value and the new value, and then sets only if they are different.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Now, in Ruby's == is easily overridable, you can easily make it do anything you want. It happens that Array#== returns true iff both arrays have the same number of elements and each element in one array is == to the element of the other array in the same position. It also happens that ActiveRecord's base class' == returns true as long as the objects are of the same class and the primary keys equal.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Which brings us to the gotcha of the day. The code in question built an array, say &lt;span class="Apple-style-span" style="font-style: italic;"&gt;new_&lt;/span&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;friend_list&lt;/span&gt;&#160;independently, starting with an id list passed in from a from, finding each &lt;span class="Apple-style-span" style="font-style: italic;"&gt;friend&lt;/span&gt;&#160;in the database and adding them to the array, but, in the meantime, modifying a field in each of the friends, say...&lt;span class="Apple-style-span" style="font-style: italic;"&gt;friend.charma++&lt;/span&gt;. It then goes on to set the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;friends&lt;/span&gt;&#160;attribute in the manner you'd already seen above, and saves &lt;span class="Apple-style-span" style="font-style: italic;"&gt;user&lt;/span&gt;. Well, the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;charma&lt;/span&gt;&#160;field&#160;that was modified in each of the friends retrieved did not get saved, because the set did not happen: the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;new_friend_list &lt;/span&gt;pointed to the same list of friends &lt;span class="Apple-style-span" style="font-style: italic;"&gt;user&lt;/span&gt;&#160;was already associated to.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Hmm..., yeah. Watch out for this one. Debugging this one was not fun. It definitely didn't fit with the principle of least surprise.&lt;/div&gt;</description>
      <pubDate>Fri, 01 May 2009 23:18:36 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/ActiveRecord_Gotcha</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/ActiveRecord_Gotcha</link>
    </item>
    <item>
      <title>Auto-mixin in Python</title>
      <description>Python has multiple inheritence - a feature that's really handy because it allows the programing idiom called &lt;span class="Apple-style-span" style="font-style: italic;"&gt;mixins&lt;/span&gt;&#160;- where you write a a bunch of methods on a "mixin" class and then later on attaching all of those methods onto another class simply by mixin-it-in. In Python, the process of mixin-it-in is simply to add it to the parent list of the class:&lt;div&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: monospace; font-size: 80%; background-color: rgb(238, 238, 238); padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; "&gt;class MyObject(MyParent, MyMixin):&lt;/pre&gt;&lt;pre style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: monospace; font-size: 80%; background-color: rgb(238, 238, 238); padding-top: 0.2em; padding-right: 0.8em; padding-bottom: 0.2em; padding-left: 0.8em; "&gt;   ...&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;In Ruby, you can automatically mix in a mixin programmatically because the mix-it-in is done within the class definition, and because of its open classes. This is how the rails helpers are automatically found in a directory and mixed into the controllers for example.&lt;/div&gt;&lt;div&gt;How would you do this in Python? Let's say we simplify the problem to this: For a given Controller subclass named ProductController, for example, if there exists a helper class by naming convention: ProductControllerHelper, then we mix it in. Here's my solution:&lt;/div&gt;&lt;pre&gt;class Meta(type):
    def __new__(cls, name, bases, dct):
        helper_mixin = globals().get('%sHelper' % name)
        if helper_mixin:
            bases = list(bases)
            bases.append(helper_mixin)
            return type.__new__(cls, name, tuple(bases), dct)
        else:
            return type.__new__(cls, name, bases, dct)
        
class Controller(object):
    __metaclass__ = Meta
        
class ProductControllerHelper(object):
    def method1(self):
        print "method1 invoked"
        
class ProductController(Controller):
    def index(self):
        self.method1()
        
        
if __name__ == '__main__':
    ProductController().index()&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Output:&lt;/div&gt;&lt;pre&gt;method1 invoked&lt;br&gt;&lt;/pre&gt;&lt;div&gt;It worked!&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;But! We are not done. What if what you want to mix in depends on how you configure the class inside the class definition? In my case, I wanted to mix in a bunch of classes when I write a DSL-like declaration like this:&lt;/div&gt;&lt;pre&gt;class MyTestCase(TestCase):
    all = fixture(SeleniumRCServer, BrowserSession, DBData, Login)
    each = fixture(AddDelivery)&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;    ...&lt;/pre&gt;&lt;div&gt;This is for a testing framework I am working on. I have a test case, and I want to specify its test context(fixtures) in the manner shown. Each object in the fixture(..) definition is a mixin. So I want to mix them all into MyTestCase.&lt;/div&gt;&lt;div&gt;This case is harder than the previous case because when __new__ is invoked, the class definition hasn't been processed yet, and therefore you won't have access to the class attributes &lt;span class="Apple-style-span" style="font-style: italic;"&gt;all &lt;/span&gt;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;each&lt;/span&gt;. The hack I came up with is this:&lt;/div&gt;&lt;pre&gt;class TestCaseMetaClass(type):
    def __init__(cls, name, bases, dct):
        if len(bases) == 1 and bases[0] != unittest.TestCase:
            bases = list(bases)
            bases.extend(cls.all)
            bases.extend(cls.each)
            cls.__real__ = type(name, tuple(bases), dct)
        else:
            type.__init__(name, bases, dct)
        
    def __call__(cls, *params, **kws):
        if hasattr(cls, '__real__'):
            return cls.__real__(*params, **kws)
        else:
            return type.__call__(cls, *params, **kws)
            
class TestCase(unittest.TestCase):
    __metaclass__ = TestCaseMetaClass&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Basically, I first override __init__ of the metaclass(rather than __new__) in order to record what was declared in &lt;span class="Apple-style-span" style="font-style: italic;"&gt;all &lt;/span&gt;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;each&lt;/span&gt;, and create a new class on the fly mixing in all the mixins defined in the fixture(..) definitions. We store that class in an attribute __real__ attached to the class. Then we override __call__ for the metaclass to return an instance of the class held in the __real__ attribute instead of the actual class.&#160;Oh yes, this is really ugly, but it worked, at least for what I was doing. If you can write to a classes' __bases__ attribute(which defines the parents of the class), then this would be much easier, but I haven't figured out how to do it if it can be done at all. But because of this limitation, it looks unlikely you can do something like Ruby's&#160;&lt;a href="http://www.somethingnimble.com/bliki/mixology"&gt;mixology&lt;/a&gt;&#160;in Python.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;U&lt;span class="Apple-style-span" style="font-style: italic;"&gt;pdate: the better solution to the second part is found:&lt;/span&gt;&lt;/div&gt;&lt;pre&gt;class Meta2(type):
    def __new__(cls, name, bases, dct):
        mixins = []
        all = dct.get('all')
        if all:
            mixins.extend(all)
        each = dct.get('each')
        if each:
            mixins.extend(each)
        bases = list(bases)
        bases.extend(mixins)
        return type.__new__(cls, name, tuple(bases), dct)
        
class TestCase(object):
    __metaclass__ = Meta2
    
class Fixture1(object):
    def method2(self):
        print "method2 invoked"
    
class Fixture2(object):
    def method3(self):
        print "method3 invoked"
        
class BlahBlahTest(TestCase):
    all = [Fixture1]
    each = [Fixture2]
    def blah_test(self):
        self.method2()
        self.method3()&lt;br&gt;&lt;/pre&gt;</description>
      <pubDate>Sun, 18 Jan 2009 00:03:16 -0600</pubDate>
      <guid>http://wiki.futuretoby.com/Auto-mixin_in_Python</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Auto-mixin_in_Python</link>
    </item>
    <item>
      <title>Ruby Gotcha of the Day</title>
      <description>Okay, so in ruby:&lt;br&gt;&lt;pre&gt;&gt;&gt;&#160;nil&#160;or&#160;'value'&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;=&gt;&#160;"value"&lt;br&gt;&lt;/pre&gt;because nil acts as false when used with boolean operators and any other object acts as true. Now let's assign the result to a temp variable a:&lt;br&gt;&lt;pre&gt;&gt;&gt; a = nil or 'value'&lt;br&gt;&lt;/pre&gt;&lt;font face="courier new,courier"&gt;&lt;pre&gt;=&gt; "value"&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;&gt;&gt; a&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;=&gt; nil&lt;br&gt;&lt;/pre&gt;&lt;/font&gt;Whoa?? Can you guess? In ruby the assignment operator is not special. It acts just like any other operator and does not have a lower precedence than the &lt;i&gt;or&lt;/i&gt; operator. Therefore the expression&lt;br&gt;&lt;pre&gt;&lt;span class="Apple-style-span" style="font-family: 'courier new';"&gt;a = nil&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;executes first, which evaluates to &lt;i&gt;nil&lt;/i&gt;, and assigns the value &lt;i&gt;nil&lt;/i&gt; to &lt;i&gt;a&lt;/i&gt;, and then the result &lt;i&gt;nil&lt;/i&gt; is used in the expression &lt;i&gt;nil or 'value'&lt;/i&gt;. Therefore to do what we intended we need to do:&lt;br&gt;&lt;pre&gt;&gt;&gt; a = (nil or 'value')&lt;br&gt;&lt;/pre&gt;&lt;font face="courier new,courier"&gt;&lt;pre&gt;=&gt; "value"&lt;br&gt;&lt;/pre&gt;&lt;/font&gt;Wow! That is messed up. &lt;br&gt;</description>
      <pubDate>Fri, 26 Dec 2008 19:03:23 -0600</pubDate>
      <guid>http://wiki.futuretoby.com/Ruby_Gotcha_of_the_Day</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Ruby_Gotcha_of_the_Day</link>
    </item>
    <item>
      <title>Impressions of Turbogears 4 months in</title>
      <description>I've been a user of Turbogears for 4 months now, working on a client facing app. The app has not gone production yet, so I don't have much insight on deployment, but I have a lot of experience on the development side - I started from scratch - and here is what I learned so far.&lt;br&gt;&lt;br&gt;&lt;b&gt;Freedom of Choice&lt;/b&gt;&lt;br&gt;Turbogears as a framework is pretty agnostic of different components such as ORM, or template engine. Although there is a default choice, I found it wasn't hard to stray away from it. &lt;br&gt;&lt;br&gt;&lt;b&gt;Mako&lt;/b&gt;&lt;br&gt;I ended up choosing Mako as the template engine because, coming from rails, I felt kid and genshi were too heavyweight for my taste since they are based on XSLT and requires your markup to be valid XML before it can do anything, which obviously means there's an XML parsing step it has to do. Mako is more like erb in that it's "text-based", e.i. it's perfectly fine to render non-valid XML code. But Mako turned out to be much more than another erb. With Mako you can easily write helper template functions and reuse them everywhere. You can also write inversion-of-control template style functions which take in a partial template and calls it inside its body. It always puzzled me why you couldn't do that with erb or haml or most of the ruby template engines easily. With erb, you have to write a partial view as a separate file, but calling a partial with local parameters is inconvient, you have to write something like:&lt;br&gt;&lt;br&gt;&lt;pre&gt;render :partial =&amp;gt; 'my_control', :locals =&amp;gt; {:control_id =&amp;gt; 'con', &lt;br&gt;    :height=&amp;gt; '50px'}&lt;/pre&gt;&lt;br&gt;and since this is so inconvient, i usually end up wrapping it with a helper method like:&lt;br&gt;&lt;br&gt;&lt;pre&gt;def my_control(control_id, height)&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; render :partial =&amp;gt; 'my_control', :locals =&amp;gt; {&lt;br&gt;        :control_id =&amp;gt; control_id, :height=&amp;gt; height}&lt;/pre&gt;&lt;pre&gt;end&lt;/pre&gt;&lt;br&gt;Of course this is partially due to the culture in rails that you normally write helpers in ruby rather than in a template language. In Mako you &lt;a href="http://www.makotemplates.org/docs/syntax.html#syntax_tags_def" mce_href="http://www.makotemplates.org/docs/syntax.html#syntax_tags_def"&gt;don't have to do this extra step&lt;/a&gt;, which makes me happy. Now if you want to do the inversion of control thing, in erb it's &lt;a href="http://www.igvita.com/2007/03/15/block-helpers-and-dry-views-in-rails/" mce_href="http://www.igvita.com/2007/03/15/block-helpers-and-dry-views-in-rails/"&gt;even worse&lt;/a&gt;! In mako you would just &lt;a href="http://www.makotemplates.org/docs/defs.html#defs_defswithcontent" mce_href="http://www.makotemplates.org/docs/defs.html#defs_defswithcontent"&gt;do this&lt;/a&gt;. So, in general, I am able to refactor my views a lot easier with mako and therefore I find myself doing it a lot more often.&lt;br&gt;&lt;br&gt;&lt;b&gt;SQLAlchemy&lt;/b&gt;&lt;br&gt;SQLAlchemy is a main stream ORM in the python community. It's direction is different from that of ActiveRecord and is a lot more similar to Hibernate of Java but also has similarities to Ambition of Ruby and Linq of .NET. It is similar to Hibernate in that it is very fully featured, has sessional transaction management, and can coupe with a large variety of schemas. It is similar to ambition and linq in that you can contruct queries in your host language in a very succint and elegant way(I know you can build queries in Hibernate's criteria API too, but it's not quite elegant). I like SQLAlchemy a lot! Here's a couple of sqlalchemy tricks I like. First one:&lt;br&gt;&lt;font face="courier new,courier"&gt;&lt;br&gt;&lt;/font&gt;&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fields = [&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User.user_name,&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User.display_name,&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; User.email_address&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ]&lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; results = User.query.filter(or_(*[f.ilike('%' + q + '%') &lt;br&gt;        for f in fields]))&lt;/pre&gt;&lt;br&gt;The above code does a wildcard partial string match of the string q against any of the three fields listed in the fields list. Second example:&lt;br&gt;&lt;br&gt;&lt;pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; page = User.query[10:20]&lt;/pre&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;This looks like array slicing, but no, it's slicing against the query results! It's smart enough to build the query using OFFSET and LIMIT or equivalent. You can easily do pagination with this technique.&lt;br&gt;&lt;br&gt;&lt;b&gt;Python's Named Parameters&lt;/b&gt;&lt;br&gt;Another thing I like when working with turbogears is python's &lt;a href="http://tobyho.com/parameter_list_chaining_in_python"&gt;named parameters&lt;/a&gt;. Whereas rubists use the hash as the poor man's name parameters, python has real named parameters, which is not only &lt;a href="/How_safe_is_your_programming_language" mce_href="/How_safe_is_your_programming_language"&gt;safer&lt;/a&gt;, but more elegant.&lt;br&gt;&lt;br&gt;&lt;b&gt;Python's Polluted Name Space&lt;/b&gt;&lt;br&gt;I run into this problem once in a while, but I hit on it 2 or 3 times in the last week! In python, &lt;i&gt;list&lt;/i&gt;, &lt;i&gt;dict&lt;/i&gt;, &lt;i&gt;str&lt;/i&gt;, &lt;i&gt;int&lt;/i&gt;, etc. are the names of fundamental types in the language, therefore you can't(actually you can, but don't want to) use them as variable names. More than once I've tried to use &lt;i&gt;list&lt;/i&gt; as a variable name, which python doesn't complain about immediately but causes a cryptic error down the road. &lt;br&gt;I've also tried use &lt;i&gt;from&lt;/i&gt; as a variable name, which turns out to be a keyword in the language, this causes a syntax error, which you don't see until you realize it's a keyword. Now, of course, more languages suffer from this problem, ruby isn't any different, but I think ruby has better error messages for these syntax errors: it will tell you the symbol that is unexpected and what symbols it was expecting.&lt;br&gt;&lt;br&gt;&lt;b&gt;Little Verbosity&lt;/b&gt;&lt;br&gt;Turbogears is more verbose than rails most prominently in 2 areas: import statements and method decorators. Rails files, usually have at most 2 requires(counterpart of import in python), most of the time none at all. My TG controller files and the model.py(the file with all the model objects) usually have about 10 to 15 lines of includes, my Mako template files usually 2 to 4 lines of includes. This has a lot to do with the design of the language. The python interpreter requires each .py file to act like a module, and as a module, it must identify all of its dependences, the ruby interpreter does not require this and so your controller code, for example, doesn't need to explicitly require anything before it has what it needs to do its work.&lt;br&gt;I think method decorators are cool, but they can also be overused and become cluttery. Some of my controller methods have more than 4 or 5 lines of decorators, consisting of the mandatory expose(), access restriction spec, and form parameter validators. That's a bit much. I also don't like the fact that you need an expose() decorator for every single controller method. Rails has no such thing and usually specifies such things at the top of the class, which has pros and cons vs TG's approach, but is at the end less cluttery.&lt;br&gt;&lt;br&gt;</description>
      <pubDate>Thu, 17 Jul 2008 22:53:02 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Impressions_of_Turbogears_4_months_in</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Impressions_of_Turbogears_4_months_in</link>
    </item>
    <item>
      <title>Getting the Argument Names of Your Function</title>
      <description>In Javascript:&lt;br&gt;&lt;font face="courier new,courier"&gt;myfunction.toString()&lt;/font&gt;&lt;br&gt;This gives you the entire definition of the function as a string, with which you can parse to get the argument names, or in prototype.js you can just do:&lt;br&gt;&lt;font face="courier new,courier"&gt;myfunction.argumentNames()&lt;/font&gt;&lt;br&gt;&lt;br&gt;In Python:&lt;br&gt;&lt;font face="courier new,courier"&gt;myfunction.func_code.co_varnames&lt;/font&gt;&lt;br&gt;&lt;br&gt;In Ruby, there is no easy way to do this, although there is A way, which involves setting a trace function on the function and then executing it to get at the local variables at execution time, see &lt;a href="http://eigenclass.org/hiki/method+arguments+via+introspection" mce_href="http://eigenclass.org/hiki/method+arguments+via+introspection"&gt;here&lt;/a&gt;.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;</description>
      <pubDate>Tue, 20 May 2008 23:50:57 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Getting_the_Argument_Names_of_Your_Function</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Getting_the_Argument_Names_of_Your_Function</link>
    </item>
    <item>
      <title>How Safe is Your Programming Language</title>
      <description>First, let me define what &lt;i&gt;I&lt;/i&gt; mean by safe: &lt;i&gt;the earlier a programming language catches a programming error for you, the safer it is&lt;/i&gt;. Haskell is extremely safe, whereas php is extremely unsafe. Some examples:&lt;br&gt;&lt;ul&gt;&lt;li&gt;Errors can be caught at compile time, such as mispelling of function names&lt;/li&gt;&lt;li&gt;Errors can be caught at runtime, such as NullPointerExceptions, but it may be caught early or later in runtime, there's a continum&lt;/li&gt;&lt;li&gt;Errors can be caught a type time if you have a method completion capable IDE, for exmple&lt;/li&gt;&lt;li&gt;Errors can be caught a unit test time, but that out of the scope for this article, because I want to talk about languages and not testing practices&lt;/li&gt;&lt;li&gt;Errors can be caught at QA testing time vs in production, these are also a bit out of scope&lt;/li&gt;&lt;/ul&gt;How do you measure the safety of a programming language? One can take specfic classes of errors and see how and when a language catches them. I will use the notation &lt;i&gt;&amp;gt;&amp;gt;&lt;/i&gt; to mean &lt;i&gt;safer than&lt;/i&gt;. A score is given for each language for each type of error corresponding to how safe they are.&lt;br&gt;&lt;br&gt;&lt;b&gt;Null Pointers&lt;/b&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Java, Ruby, Python, C#, Smalltalk, Javascript, LISP - catch at runtime when you try to derefernce the pointer&lt;/li&gt;&lt;li&gt;C, C++ - never catches it, they merely core dump, which does not tell you what the specific error was&lt;/li&gt;&lt;li&gt;Haskell, OCaml - catches it at compile time, because you cannot have Null values unless you specifically define it in the type, and the compiler requires you to handle the null case&lt;/li&gt;&lt;/ul&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; So in this case: &lt;b&gt;&lt;font color="#99cc00"&gt;Haskell, OCaml (2) &amp;gt;&amp;gt; Java, Ruby, Python, C#, Smalltalk, Javascript, LISP (1) &amp;gt;&amp;gt; C, C++ (0)&lt;/font&gt;&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Array Index Out Of Bounds&lt;/b&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Java, Python, C#, Smalltalk - catch at runtime when you try to index the array&lt;/li&gt;&lt;li&gt;Ruby, Javascript - never catches it, merely returns null if you try to index an array with an out of bounds index&lt;/li&gt;&lt;li&gt;C, C++ - never catches it, may or may not core dump.&lt;br&gt;&lt;/li&gt;&lt;li&gt;Haskell, LISP, OCaml - although you can use Arrays if you really want/need to, in which case the error is caught at runtime like Java, etc. The prefered practice of these languages is to use linked-lists instead, which will never have index out of bounds errors&lt;/li&gt;&lt;/ul&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; So: &lt;b&gt;&lt;font color="#99cc00"&gt;Haskell, LISP, OCaml (2) &amp;gt;&amp;gt; Java, Python, C#, Smalltalk (1) &amp;gt;&amp;gt; Ruby, Javascript, C, C++ (0)&lt;/font&gt;&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Type Errors&lt;/b&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Java, C# - these are caught at compile time &lt;i&gt;if&lt;/i&gt; you never use casting in your program. &lt;i&gt;But&lt;/i&gt; if you do use casting, these will be caught at runtime as ClassCastExceptions at time of cast.&lt;/li&gt;&lt;li&gt;Ruby, Python, Javascript, Smalltalk, LISP - caught at runtime when a method on the object is accessed&lt;/li&gt;&lt;li&gt;C, C++ - Compile time if you don't use casting. Never caught and possible core dumps if you do.&lt;/li&gt;&lt;li&gt;Haskell, OCaml - Compile time always, casting is not allowed.&lt;/li&gt;&lt;/ul&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;font color="#99cc00"&gt;Haskell, OCaml (3) &amp;gt;&amp;gt; Java, C# (2) &amp;gt;&amp;gt; Ruby, Python, Javascript, Smalltalk, LISP (1) &amp;gt;&amp;gt; C, C++ (0)&lt;/font&gt;&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Mispelled Named Parameters&lt;/b&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Java, C#, C, C++, Haskell, Javascript - these languages do not have this feature&lt;/li&gt;&lt;li&gt;Python, Smalltalk - caught at time of method invocation&lt;/li&gt;&lt;li&gt;Ruby - rubists employ a poor-man's named parameters by just passing in a Hash to methods, the languages supports syntactic sugar that make this style of method calling look like named parameters, but mispelled keys are never caught&lt;/li&gt;&lt;li&gt;OCaml - caught at compile time&lt;/li&gt;&lt;/ul&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;font color="#99cc00"&gt;OCaml (2) &amp;gt;&amp;gt; Python, Smalltalk (1) &amp;gt;&amp;gt; Ruby (0)&lt;/font&gt;&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Wrong Number of Parameters&lt;/b&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Python, Smalltalk, LISP - caught at method invocation&lt;/li&gt;&lt;li&gt;Javascript - never caught, unsupplied parameters an simple set to null, and it never hurts to use too many parameters&lt;/li&gt;&lt;li&gt;Ruby - caught at method invocation time for the normal cases, but blocks are a special type of parameter which is always optional. So if you supply a block to a method that does not require it, ruby does not complain&lt;/li&gt;&lt;li&gt;Haskell, Java, C#, C, C++ - compile time&lt;/li&gt;&lt;/ul&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;&lt;font color="#99cc00"&gt;Haskell, OCaml, Java, C#, C, C++ (3) &amp;gt;&amp;gt; Python, Smalltalk, LISP (2) &amp;gt;&amp;gt; Ruby (1) &amp;gt;&amp;gt; Javascript (0)&lt;/font&gt;&lt;/b&gt;&lt;br&gt;&lt;br&gt;So a language can be safer or less safe under different contexts. There obviously are other types of errors that I have not listed, but this will do for a simple comparison. I've already given scores for each language for each error type. Since some languages do not have the named parameters feature, I will average the score for each language over the error types that are relevant to it. So the Tally is:&lt;br&gt;&lt;ol&gt;&lt;li&gt;Haskell - scores: [2, 2, 3, 3], average: 2.5&lt;/li&gt;&lt;li&gt;OCaml - scores: [2,2,3,2,3], average: 2.4&lt;br&gt;&lt;/li&gt;&lt;li&gt;Java, C# - scores: [1,1,2,3], average: 1.75&lt;/li&gt;&lt;li&gt;LISP - scores: [1,2,1,2], average: 1.5&lt;br&gt;&lt;/li&gt;&lt;li&gt;Python, Smalltalk - scores: [1,1,1,1,2], average: 1.2&lt;/li&gt;&lt;li&gt;C, C++ - scores: [0,0,0,3], average: 0.75&lt;br&gt;&lt;/li&gt;&lt;li&gt;Ruby - scores: [1,0,1,0,1], average: 0.6&lt;/li&gt;&lt;li&gt;Javascript - [1,0,1,0], average: 0.5&lt;br&gt;&lt;/li&gt;&lt;/ol&gt;It's not really fair that OCaml is scoring lower than Haskell, they should be the same, the difference is because Haskell doesn't have named parameters so the average is skewed - my scoring system is not perfect. Haskell is safer than OCaml in one aspect though. Which is that in Haskell, purely functional code is guaranteed to have no side effects and will never throw exceptions.&lt;br&gt;</description>
      <pubDate>Sun, 30 Mar 2008 12:02:23 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/How_Safe_is_Your_Programming_Language</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/How_Safe_is_Your_Programming_Language</link>
    </item>
    <item>
      <title>Ruby: Extremely Unsafe</title>
      <description>I have been making the same mistake a few times recently: instead of writing&lt;br&gt;&lt;font face="courier new,courier"&gt;things.each do |thing| ... end&lt;/font&gt;&lt;br&gt;Where things is a method, not a variable, I would write:&lt;br&gt;&lt;font face="courier new,courier"&gt;things do |thing| ... end&lt;/font&gt;&lt;br&gt;which has the end result of doing nothing. And I would have a hard time tracking down the bug - even when I have tests written which are failing because of it. I think it's because you just don't &lt;i&gt;see &lt;/i&gt;a mistake like this, it's just one of those things where you can stare at the line all day and still not have a clue. Admittedly, this is a stupid mistake, but I also blame ruby for being extremely lose in its method invocation behavior when dealing with blocks. A block on a method is like an optional argument on ALL methods, not just the ones where you mean to use blocks. So this mean you can pass a block to any method, even if it's as simple as:&lt;br&gt;&lt;font face="courier new,courier"&gt;def hello&lt;br&gt;&amp;nbsp; 'hello'&lt;br&gt;end&lt;/font&gt;&lt;br&gt;I have enumerated a handful of other languages including Smalltalk, Python, Haskell, Lisp, Java, and none of them behave this way. This is just one example of why Ruby is on the &lt;i&gt;extremely unsafe&lt;/i&gt; end of the language spectrum. And this is also why there's a bigger need for automated testing in Ruby software, because the intepreter is so forgiving that even little mistakes like these - which are normally easily caught at runtime, if not compile time - can go through undetected.&lt;br&gt;&lt;br&gt;Another unsafe thing about Ruby software is the frequent use of hashes as the poor man's &lt;i&gt;named parameters&lt;/i&gt;. If you misspell the name of the key, there would be no complaint. And when you find out something's wrong, you wouldn't know where to look first.&lt;br&gt;&lt;br&gt;Yet another is the fact that arrays simply return nil when you give it an out-of-bounds index:&lt;br&gt;&lt;font face="courier new,courier"&gt;&amp;gt;&amp;gt; [][7]&lt;br&gt;=&amp;gt; nil&lt;/font&gt;&lt;br&gt;&lt;br&gt;I guess being unsafe is just part of the Ruby culture. In related news: Javascript is extremely unsafe too, arguably even more so than Ruby, since, for example it's not strict about enforcing the number of parameters you can pass into a function..&lt;br&gt;&lt;br&gt;</description>
      <pubDate>Sat, 15 Mar 2008 22:01:32 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Ruby%3A_Extremely_Unsafe</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Ruby%3A_Extremely_Unsafe</link>
    </item>
    <item>
      <title>Autotest popup notifications with Snarl</title>
      <description>After learning of growl from &lt;a href="http://blog.aisleten.com/2008/02/21/installing-growlnotify-and-autotest-for-bdd-use-with-rspec-on-leopard/" mce_href="http://blog.aisleten.com/2008/02/21/installing-growlnotify-and-autotest-for-bdd-use-with-rspec-on-leopard/"&gt;Micah's post&lt;/a&gt;, I decided to port it to the windows equivalent: snarl. It was pretty easy:&lt;br&gt;&lt;ol&gt;&lt;li&gt;Follow Micah's instructions except for the growl bits&lt;br&gt;&lt;/li&gt;&lt;li&gt;Install Snarl from their website, this is a setup .exe&lt;/li&gt;&lt;li&gt;gem install ruby-snarl&lt;/li&gt;&lt;li&gt;get the icon images from Micah's post and put them some where. I put them in &lt;font face="courier new,courier"&gt;c:/devtools/autotest redgreen icons/&lt;/font&gt;&lt;/li&gt;&lt;li&gt;Put the code below in your .autotest, in place of the stuff for growl:&lt;/li&gt;&lt;/ol&gt;&lt;br&gt;&lt;font face="courier new,courier"&gt;require 'autotest/redgreen' &lt;br&gt;require 'snarl'&lt;br&gt;module Autotest::Growl &lt;br&gt;&amp;nbsp; def self.notify title, msg, img&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Snarl.show_message title, msg, img&lt;br&gt;&amp;nbsp; end &lt;br&gt;&amp;nbsp;&lt;br&gt;&amp;nbsp; Autotest.add_hook :ran_command do |autotest| &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; icon_path = 'c:/devtools/autotest redgreen icons/'&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; output = autotest.results.grep(/\d+\s.*examples?/).last.slice(/(\d+)\s.*examples?,\s(\d+)\s.*failures?(?:,\s(\d+)\s.*pending)?/) &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if output =~ /[1-9]\sfailures?/ &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; notify "Test Results", "#{output}", "#{icon_path}rails_fail.png" &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; elsif output =~ /pending/ &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font face="courier new,courier"&gt;notify &lt;/font&gt;&lt;font face="courier new,courier"&gt;"Test Results", "#{output}", "#{icon_path}rails_pending.png" &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font face="courier new,courier"&gt;notify &lt;/font&gt;&lt;font face="courier new,courier"&gt;"Test Results", "#{output}", "#{icon_path}rails_ok.png" &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br&gt;&amp;nbsp; end&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br&gt;end &lt;br&gt;&lt;/font&gt;&lt;br&gt;&lt;br&gt;Screenshot:&lt;br&gt;&lt;img src="http://wiki.futuretoby.com/uploads/snarl.jpg?1205507866" name="snarl.jpg" width="364"&gt;&lt;br&gt;</description>
      <pubDate>Fri, 14 Mar 2008 10:06:50 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Autotest_popup_notifications_with_Snarl</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Autotest_popup_notifications_with_Snarl</link>
    </item>
  </channel>
</rss>

