Jim Weirich wrote:

> On a side note, this whole discussion inspired me to solve the problem in
> an interesting way.  I just make sure I initialize the database object
> with an instance of the following class:
> 
>   class MissingDatabase
>     def method_missing(sym, *args, &block)
>       fail "No Database Specified"
>     end
>   end

Very nice! Some elaboration:

$ cat missing_obj.rb
class MissingObject < StandardError
   def initialize(comment="")
     @comment = comment
     @location = caller[0][/.*?:\d+/]
   end
   def method_missing(meth, *args, &block)
     raise self, "#{@location}: #{@comment}"
   end
end

class MissingDatabase < MissingObject; end

def get_db_from_user_input
end

# Using nil:
my_db = get_db_from_user_input()
begin
   my_db.lookup("foo")
rescue => e
   p e
end

# Using a MissingObject
my_db = get_db_from_user_input() ||
         MissingDatabase.new("No user input")
begin
   my_db.lookup("foo")
rescue => e
   p e
end

$ ruby missing_obj.rb
#<NoMethodError: undefined method `lookup' for nil:NilClass>
#<MissingDatabase: missing_obj.rb:26: No user input>