Le 6/5/2005, "Jim Weirich" <jim / weirichhouse.org> a ?crit:
>
>Austin Ziegler said:
>> For a very small class of problems, this will work. For anything
>> that has to interact outside of the Ruby program in question, this
>> is -- and always will be -- a disaster.
>
>As I mentioned before, I'm in the "throw an exception" camp.  But the
>argument that it will be a "disaster" sounds like the type of arguement a
>static typer levels againts dynamic languages.  So, I'm open to an
>experiment.
>
>And the first opportunty came up today.  I am revising some command line
>scripts that access a database.  You have to specify an environment so
>that the proper database case be choson.  I forgot to specify the
>environment and got:
>
>  $ ruby -Ilib bin/info.rb r887
>  lib/ics/folderid.rb:83:in `translate_rids': undefined method `sql' for
>nil (NameError)
>
>Then I added the -rnilify argument...
>
>  $ ruby -rnilify -Ilib bin/info.rb r887
>  Can't find folder for ID r887
>
>So, adding in a message eating nil causes the script to think it can't
>find the appropriate folder.  While true, it is certainly not the best
>error message that could be produced.  I would imagine that tracing down
>the source of this error would be more difficult than the other message. I
>knew exactly what the problem was when I saw the NameError.  The "can't
>find" error leaves plenty of room for other possibilities.
>
>So, at first blush, I'm still in the "throw an exception" camp.

Actually, throwing an exception might be better to do at an earlier
point. Essentially two types of nils exists; the expected ones and
the unexpected ones, of which the latter cause the problems here.
Most of these problems could be 'avoided', however, if instead
of returning nil, an exception were thrown.

>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
>--
>-- Jim Weirich     jim / weirichhouse.org    http://onestepback.org

E

--
template<typename duck>
void quack(duck& d) { d.quack(); }