[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

assignment if key_exist? in Hash

darren kirby

4/1/2008 3:06:00 AM

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
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

7 Answers

Peña, Botp

4/1/2008 3:30:00 AM

0

From: darren kirby [mailto:bulliver@badcomputer.org]=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


darren kirby

4/1/2008 3:45:00 AM

0

quoth the Pe=F1a, Botp:
> From: darren kirby [mailto:bulliver@badcomputer.org]
> # > 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://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

ara.t.howard

4/1/2008 3:57:00 AM

0


On Mar 31, 2008, at 9:30 PM, Pe=F1a, Botp wrote:
> foo =3D some_hash["some_key"] || foo

that replaces even if the key is set to 'false', 'nil', or if the has =20=

does not have the key... but prolly what OP wants...


regards.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being =20
better. simply reflect on that.
h.h. the 14th dalai lama




darren kirby

4/1/2008 4:16:00 AM

0

quoth the ara.t.howard:
> On Mar 31, 2008, at 9:30 PM, Pe=F1a, Botp wrote:
> > 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://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

Julian Leviston

4/1/2008 4:25:00 AM

0

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

On 01/04/2008, at 3:15 PM, darren kirby wrote:

> quoth the ara.t.howard:
>> On Mar 31, 2008, at 9:30 PM, Pe=F1a, Botp wrote:
>>> 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://badco...
> "...the number of UNIX installations has grown to 10, with more =20
> expected..."
> - Dennis Ritchie and Ken Thompson, June 1972
>




darren kirby

4/1/2008 4:36:00 AM

0

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
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

Daniel DeLorme

4/1/2008 6:29:00 AM

0

darren kirby wrote:
> 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.

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

--
Daniel