PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > assignment if key_exist? in Hash
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
assignment if key_exist? in Hash

Réponse
 
LinkBack Outils de la discussion
Vieux 01/04/2008, 04h05   #1
darren kirby
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut assignment if key_exist? in Hash

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://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

  Réponse avec citation
Vieux 01/04/2008, 04h30   #2
Peña, Botp
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assignment if key_exist? in Hash

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


  Réponse avec citation
Vieux 01/04/2008, 04h45   #3
darren kirby
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assignment if key_exist? in Hash

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://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

  Réponse avec citation
Vieux 01/04/2008, 04h56   #4
ara.t.howard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assignment if key_exist? in Hash


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://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being =20
better. simply reflect on that.
h.h. the 14th dalai lama




  Réponse avec citation
Vieux 01/04/2008, 05h15   #5
darren kirby
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assignment if key_exist? in Hash

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://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
=2D Dennis Ritchie and Ken Thompson, June 1972

  Réponse avec citation
Vieux 01/04/2008, 05h25   #6
Julian Leviston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assignment if key_exist? in Hash

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/

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://badcomputer.org
> "...the number of UNIX installations has grown to 10, with more =20
> expected..."
> - Dennis Ritchie and Ken Thompson, June 1972
>





  Réponse avec citation
Vieux 01/04/2008, 05h36   #7
darren kirby
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assignment if key_exist? in Hash

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://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

  Réponse avec citation
Vieux 01/04/2008, 07h28   #8
Daniel DeLorme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: assignment if key_exist? in Hash

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13684 seconds with 16 queries