# Assignment if key\_exist? in Hash

**URL:** <https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503>\
**Category:** Ruby\
**Created:** [April 1, 2008, 5:06am UTC](https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503 "2008-04-01T05:06:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [April 1, 2008, 5:06am UTC](https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503/1 "2008-04-01T05:06:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [April 1, 2008, 5:31am UTC](https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503/2 "2008-04-01T05:31:10Z")

</div>

From: darren kirby [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# \> foo = some\_hash[“some\_key”] if some\_hash.has\_key?(“some\_key”)

try,

foo = some\_hash[“some\_key”] || foo

kind regards -botp

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [April 1, 2008, 5:57am UTC](https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503/3 "2008-04-01T05:57:31Z")

</div>

On Mar 31, 2008, at 9:30 PM, Peña, Botp wrote:

> foo = some\_hash[“some\_key”] || foo

that replaces even if the key is set to ‘false’, ‘nil’, or if the has  
does not have the key… but prolly what OP wants…

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [April 1, 2008, 5:45am UTC](https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503/4 "2008-04-01T05:45:48Z")

</div>

quoth the Peña, Botp:

> From: darren kirby [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> # \> foo = some\_hash[“some\_key”] if some\_hash.has\_key?(“some\_key”)
> 
> try,
> 
> foo = some\_hash[“some\_key”] || foo
> 
> kind regards -botp

That’s the one.  
Thanks botp,

-d

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [April 1, 2008, 6:16am UTC](https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503/5 "2008-04-01T06:16:16Z")

</div>

quoth the ara.t.howard:

> On Mar 31, 2008, at 9:30 PM, Peña, Botp wrote:
> 
> > foo = 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…

I don’t understand…If the hash key doesn’t exist then foo retains its  
original value. That is the behaviour I require:

> foo = “Some String”  
> h = {}  
> foo = h[“none\_such”] || foo  
> =\> “Some String”  
> h = {“some\_key”=\>“some value”}  
> foo = h[“some\_key”] || foo  
> =\> “some value”

-d

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [April 1, 2008, 6:26am UTC](https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503/6 "2008-04-01T06:26:14Z")

</div>

What he’s saying is if you want to retain the value of “false” or  
“nil”, that won’t work too well

Learn about Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW  
VIDEO OUT 3rd MARCH  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [April 1, 2008, 6:36am UTC](https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503/7 "2008-04-01T06:36:46Z")

</div>

quoth the Julian L.:

> 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

---

<div class="post-metadata">

**Author:** ![darren\_kirby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ed8c4c/32.png) [@darren\_kirby](https://www.ruby-forum.com/u/darren_kirby)\
**Post date:** [April 1, 2008, 8:29am UTC](https://www.ruby-forum.com/t/assignment-if-key-exist-in-hash/133503/8 "2008-04-01T08:29:26Z")

</div>

darren kirby wrote:

> quoth the Julian L.:
> 
> > 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)
