Hi,
I use `nil's for light failures, and exceptions for heavier failures.
When I couldn't decide, I provide both (e.g. IO#gets and IO#readline).
In message "[ruby-talk:03077] Re: FailureClass?"
on 00/06/01, Quinn Dunkan <quinn / mark.ugcs.caltech.edu> writes:
|returns (which is why I'd expect an invalid hash lookup to throw an exception
|rather than return nil, since a hash should be able to store 'nil's, but maybe
|I'm too used to python)).
In 1.4, `nil's are not valid value for Hash. Making associating value
to nil means deleting the association as a whole.
a = {"a" => "A", "b" => "B"}
a["b"] = nil
p a # => {"a" => "A"}
In 1.5 and later, hash can store `nil's.
a = {"a" => "A", "b" => "B"}
a["b"] = nil
p a # => {"a" => "A", "b" => nil}
Hash#fetch returns IndexError exception for lookup failure.
|While I'm on the subject of hashes, could anyone tell me why:
|
|% cat >hash.rb
|d = {'a' => 1, 'b' => 2}
|p d
|d[10] = nil
|p d
|% ruby hash.rb
|{"a"=>1, "b"=>2}
|{"a"=>1}
|%
|
|What happened to d['b'] ?
Which version are you using?
On my linux box, 1.4.4 gives:
{"a"=>1, "b"=>2}
{"a"=>1, "b"=>2}
while 1.5.4 gives:
{"a"=>1, "b"=>2}
{"a"=>1, 10=>nil, "b"=>2}
matz.