"William Djaja Tjokroaminata" <billtj / y.glue.umd.edu> wrote in message
news:an01mb$la0$1 / grapevine.wam.umd.edu...
> Hi,
>
> I think it is already there.  If you first declare the hash with
>
>     files = Hash.new ("Untitled")
>
> then you will get "Untitled" instead of nil for any missing key.  (But of
> course, then you cannot create and populate the hash at the same time :)

files = { ... }
files.default="Untitled"

I just installed Ruby 1.7.2-4 for Windows, this "ri" tool, whatever it is,
is extremely useful:

C:\>ri Hash
------------------------------------------------------------------------
     class: Hash
------------------------------------------------------------------------
     A Hash is a collection of key-value pairs. It is similar to an
     Array, except that indexing is done via arbitrary keys of any
     object type, not an integer index. The order in which you traverse
     a hash by either key or value may seem arbitrary, and will
     generally not be in the insertion order.
     Hashes have a default value that is returned when accessing keys
     that do not exist in the hash. By default, that value is nil.

------------------------------------------------------------------------
     ==, [], [], []=, clear, default, default=, delete, delete_if, each,
     each_key, each_pair, each_value, empty?, fetch, has_key?,
     has_value?, include?, index, indexes, indices, invert, key?, keys,
     length, member?, new, rehash, reject, reject!, replace, shift,
     size, sort, store, to_a, to_s, update, value?, values
------------------------------------------------------------------------

C:\>ri Hash#default
----------------------------------------------------------- Hash#default
     hsh.default -> anObject
------------------------------------------------------------------------
     Returns the ``default value''---that is, the value returned for a
     key that does not exist in the hash. Defaults to nil. See also
     Hash#default=.