|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 > |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|