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