From: Ross Bamford Date: 2006-03-05T02:06:10+09:00 Subject: Re: Kirbybase Hacks NilClass On Sun, 2006-03-05 at 01:37 +0900, James Edward Gray II wrote: > I'm examining Kirbybase for use it a project and was surprised to > find this bit of code in it: > > #----------------------------------------------------------------------- > ---- > # NilClass > #----------------------------------------------------------------------- > ---- > class NilClass > > #----------------------------------------------------------------------- > # method_missing > > #----------------------------------------------------------------------- > # > # This code is necessary because if, inside a select condition code > # block, there is a case where you are trying to do an expression > # against a table field that is equal to nil, I don't want a method > # missing exception to occur. I just want the expression to be > nil. I > # initially had this method returning false, but then I had an > issue > # where I had a YAML field that was supposed to hold an Array. > If the > # field was empty (i.e. nil) it was actually returning false > when it > # should be returning nil. Since nil evaluates to false, it > works if I > # return nil. > # Here's an example: > # #select { |r| r.speed > 300 } > # What happens if speed is nil (basically NULL in DBMS terms)? > Without > # this code, an exception is going to be raised, which is not > what we > # really want. We really want this expression to return nil. > def method_missing(method_id, *stuff) > return nil > end > end > > This has been a popular discussion lately, but I just don't think > loading a database library should fundamentally change the language. > ActiveRecord doesn't hack NilClass and we seem to do okay with that. > Why can't the above example just be coded as: > > select { |r| r.speed and r.speed > 300 } > > or: > > select { |r| r.speed > 300 rescue false } > > Are Kirbybase users really liking this behavior? > > James Edward Gray II I've never used KirbyBase, but from this it looks like it could just catch NoMethodError in #select...? Introducing this new behaviour to nil could seriously suck in at least one case I can think of: while coding stuff up I often get a nomethoderror for nil telling me I'm off by one or have passed silly arguments... -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk