From: matz@... (Yukihiro Matsumoto) Date: 2000-12-04T00:15:47+09:00 Subject: [ruby-talk:6768] Re: printing/accessing arrays and hashes Hi, In message "[ruby-talk:6678] Re: printing/accessing arrays and hashes" on 00/12/01, Raja S. writes: |--------------------------------------------------------------------------- | |Problem: Array/hash access via #[] (i.e.m a[i], h[i]) does not raise an | exception for out of range indices/keys. | |Fix: Hash#fetch raises an exception but unfortunately nothing equivalent | for arrays. Probably Hash#fetch equivalent for Array is good answer. What do you guys think is the best name for it? Array#get? Array#fetch? |Quirk: Printing hash literals as the first argument to print causes | syntax problems: | | print {'a'=>1, 'b'=>2} | |Fix: Precede by an empty string. | print "", {'a'=>1, 'b'=>2} Or use parentheses. If there's good way to distinguish blocks and hashes, without confusing yacc generated parser, I'd like to know. |--------------------------------------------------------------------------- | |Problem; a[i..j / i...j / i,cnt] returns nil instead of [] for non-existing | segments. | | Creates a problem for iterators as #each isn't defined for nil. | |Fix: Don't know of a clean one. | |Comment: Either #each needs to be defined for nil [Doesn't feel right] | | Or a[i..j / i...j / i,cnt] should return the empty array, [], for out | out of bounds segments. [This feels right] | |--------------------------------------------------------------------------- This is iteresting comment. Let's discuss. If index is out of bound, what should [] return? nil or empty array? matz.