[#6363] Re: rescue clause affecting IO loop behavior — ts <decoux@...>

>>>>> "D" == David Alan Black <dblack@candle.superlink.net> writes:

17 messages 2000/11/14
[#6367] Re: rescue clause affecting IO loop behavior — David Alan Black <dblack@...> 2000/11/14

Hello again --

[#6582] best way to interleaf arrays? — David Alan Black <dblack@...>

Hello --

15 messages 2000/11/26

[#6646] RE: Array Intersect (&) question — Aleksi Niemel<aleksi.niemela@...>

Ross asked something about widely known and largely ignored language (on

23 messages 2000/11/29
[#6652] RE: Array Intersect (&) question — rpmohn@... (Ross Mohn) 2000/11/29

aleksi.niemela@cinnober.com (Aleksi Niemel) wrote in

[#6723] Re: Array Intersect (&) question — Mathieu Bouchard <matju@...> 2000/12/01

> >Use a hash. Here's code to do both and more. It assumes that

[#6656] printing/accessing arrays and hashes — raja@... (Raja S.)

I'm coming to Ruby with a Python & Common Lisp background.

24 messages 2000/11/30

[ruby-talk:6643] Re: Hash with a key of nil ?

From: Dave Thomas <Dave@...>
Date: 2000-11-29 17:21:43 UTC
List: ruby-talk #6643
rpmohn@panix.com (Ross Mohn) writes:

> While reading data in from a file and populating a hash, I accidentally 
> added a key/val pair of nil/nil. All was fine until I tried to sort the 
> hash and got "NameError: undefined method `<=>' for nil". I know I should 
> check for nils before populating a hash, but it raises two questions in my 
> mind:
> 
> 1) Should nil be allowed as a Hash key? This does not make sense to me.
> 2) Maybe there should be a method defined for nil.<=> ? I think if it 
> always returned -1 it would be appropriate.

Perhaps the alternative questions might be: should you always be able
to sort a hash based on it's keys? I suspect the answer is "no".  For
example:

    h = { STDIN => 1, STDOUT => 2 }

    STDIN <=> STDOUT
    NameError: undefined method `<=>' for #<IO:0x401900a4>


Hash keys need only provide #hash and #eql?. There's no requirement for 
<=>, so in general you can't rely on being able to sort a hash.

You could always define a class SortableHash that verified that its
key objects supported #<=> before inserting them.

Back to 'nil' as a Hash key. I can't see any reason to forbid it
(although I believe older Rubys did). For example:

   namesOfObjects = { nil => 'nil', true => 'true', 1 => 'one' }

   namesOfObjects[nil]		# => "nil"

In terms of comparisons, I thing it might be confusing to introduce an 
artificial semantic such as 'nil is less than everything'. I think
this is one of those 'ain't broke' situations.


Regards


Dave




In This Thread