Hash missing value

Wouldn’t it be useful with a missing value in hash tables so you avoid the
nil value and having to test for this?

files = { “item1” => “myfile.rb”, “item2” => “mytestdata”, missing_value =>
“Untitled”}
window.caption = files[current_item]

(this is probably implemented or conflicts with something, this is what
happens to all my wouldn’t it be nice…)

Mikkel

“MikkelFJ” mikkelfj-anti-spam@bigfoot.com wrote in message
news:3d938793$0$70352$edfadb0f@dspool01.news.tele.dk…

Wouldn’t it be useful with a missing value in hash tables so you avoid the
nil value and having to test for this?

files = { “item1” => “myfile.rb”, “item2” => “mytestdata”, missing_value
=>
“Untitled”}
window.caption = files[current_item]

(this is probably implemented or conflicts with something, this is what
happens to all my wouldn’t it be nice…)

As an extension or alternative to this, what about a two value assigment
where the second value is assigned if the first value is false or nil:

files = { “item1” => “myfile.rb”, “item2” => “mytestdata”}
window.caption = files[current_item] alternative “Untitled”
or
window.caption = files[current_item] / “Untitled”

Mikkel

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 :slight_smile:

Regards,

Bill

···

============================================================================
MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

Wouldn’t it be useful with a missing value in hash tables so you avoid
the
nil value and having to test for this?

files = { “item1” => “myfile.rb”, “item2” => “mytestdata”, missing_value =>
“Untitled”}
window.caption = files[current_item]

(this is probably implemented or conflicts with something, this is what
happens to all my wouldn’t it be nice…)

Mikkel

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 :slight_smile:

Regards,

Bill

Wouldn’t it be useful with a missing value in hash tables so you avoid
the
nil value and having to test for this?

You can set it after initialization too

files = { “item1” => “myfile.rb”, “item2” => “mytestdata”, missing_value =>
“Untitled”}

files.default = “Unititled”

···

On Fri, Sep 27, 2002 at 07:37:56AM +0900, William Djaja Tjokroaminata wrote:

MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

window.caption = files[current_item]


Alan Chen
Digikata LLC
http://digikata.com

“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 :slight_smile:

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=.

Hi,

I think this has been discussed before. We can take an advantage of, as
one instructor put it, “the short circuit concept from electrical
engineering”, i.e.,

A || B # if A is false, then B should be evaluated; otherwise B is not

and a similar concept for &&. So you can simply write:

window.caption = files[current_item] || "Untitled"

Regards,

Bill

···

============================================================================
MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

As an extension or alternative to this, what about a two value assigment
where the second value is assigned if the first value is false or nil:

files = { “item1” => “myfile.rb”, “item2” => “mytestdata”}
window.caption = files[current_item] alternative “Untitled”
or
window.caption = files[current_item] / “Untitled”

Mikkel

window.caption = (files[current_item] ||= “Untitled”)

Gavin

···

----- Original Message -----
From: “MikkelFJ” mikkelfj-anti-spam@bigfoot.com

As an extension or alternative to this, what about a two value assigment
where the second value is assigned if the first value is false or nil:

files = { “item1” => “myfile.rb”, “item2” => “mytestdata”}
window.caption = files[current_item] alternative “Untitled”
or
window.caption = files[current_item] / “Untitled”

Mikkel

window.caption = files[current_item] || "Untitled"

See that’s what I mean - everything I can think of in Ruby has already been
implemented.
Obviously I have far too few opportunities to write Ruby code, but when I
do, I get all these enlightning experiences :slight_smile:

Mikkel

window.caption = files[current_item] # nil
window.caption ||= “Untitled” # set to ‘Untitled’ if nil

:slight_smile:

···

On Friday 27 September 2002 08:18, MikkelFJ wrote:

window.caption = files[current_item] || "Untitled"

See that’s what I mean - everything I can think of in Ruby has
already been implemented.
Obviously I have far too few opportunities to write Ruby code, but
when I do, I get all these enlightning experiences :slight_smile:

Mikkel