assignment if key_exist? in Hash

D

darren kirby

Hi all,

I have a bunch of lines like:
foo = some_hash["some_key"] if some_hash.has_key?("some_key")

This feels awkward and cumbersome, so I would imagine there is a more
idiomatic way to write it.

Thanks,
-d
 
P

Peña, Botp

From: darren kirby [mailto:[email protected]]=20
# > foo =3D some_hash["some_key"] if some_hash.has_key?("some_key")

try,

foo =3D some_hash["some_key"] || foo

kind regards -botp
 
D

darren kirby

quoth the Pe=F1a, Botp:
From: darren kirby [mailto:[email protected]]
# > foo =3D some_hash["some_key"] if some_hash.has_key?("some_key")

try,

foo =3D some_hash["some_key"] || foo

kind regards -botp

That's the one.
Thanks botp,

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972
 
D

darren kirby

quoth the ara.t.howard:
foo =3D some_hash["some_key"] || foo

that replaces even if the key is set to 'false', 'nil', or if the hash
does not have the key...=20

I don't understand...If the hash key doesn't exist then foo retains its=20
original value. That is the behaviour I require:
foo =3D "Some String"
h =3D {}
foo =3D h["none_such"] || foo =3D> "Some String"
h =3D {"some_key"=3D>"some value"}
foo =3D h["some_key"] || foo
=3D> "some value"

=2Dd
=2D-=20
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972
 
J

Julian Leviston

What he's saying is if you want to retain the value of "false" or =20
"nil", that won't work too well


Learn about Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW =20=

VIDEO OUT 3rd MARCH
http://sensei.zenunit.com/

quoth the ara.t.howard:
foo =3D some_hash["some_key"] || foo

that replaces even if the key is set to 'false', 'nil', or if the =20
hash
does not have the key...

I don't understand...If the hash key doesn't exist then foo retains =20=
its
original value. That is the behaviour I require:
foo =3D "Some String"
h =3D {}
foo =3D h["none_such"] || foo =3D> "Some String"
h =3D {"some_key"=3D>"some value"}
foo =3D h["some_key"] || foo
=3D> "some value"

-d
--=20
darren kirby :: Part of the problem since 1976 :: = http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more =20
expected..."
- Dennis Ritchie and Ken Thompson, June 1972
 
D

darren kirby

quoth the Julian Leviston:
What he's saying is if you want to retain the value of "false" or
"nil", that won't work too well

Ahh, OK.

Not a prob. In my use case the hash key will be set to a string or it will not
exist.

Thanks all,
-d
 
D

Daniel DeLorme

darren said:
quoth the Julian Leviston:

Ahh, OK.

Not a prob. In my use case the hash key will be set to a string or it will not
exist.

You could also use
foo = hash.fetch("some_key", foo)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top