# Change hash key based on hash value

**URL:** <https://www.ruby-forum.com/t/change-hash-key-based-on-hash-value/261568>\
**Category:** Development\
**Created:** [July 19, 2021, 12:21pm UTC](https://www.ruby-forum.com/t/change-hash-key-based-on-hash-value/261568 "2021-07-19T12:21:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunilkumarnagoore](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e495f1/32.png) [@sunilkumarnagoore](https://www.ruby-forum.com/u/sunilkumarnagoore)\
**Post date:** [July 19, 2021, 12:21pm UTC](https://www.ruby-forum.com/t/change-hash-key-based-on-hash-value/261568/1 "2021-07-19T12:21:49Z")

</div>

hash = {  
“price”: “419.5”,  
“pricelevelname”: “STOCKING DEALER”  
}  
Expected Result  
{  
“price\_STOCKING DEALER”: “419.5”,  
“pricelevelname”: “STOCKING DEALER”,  
}

how to achieve this

---

<div class="post-metadata">

**Author:** ![G4143](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a88e57/32.png) [@G4143](https://www.ruby-forum.com/u/G4143)\
**Post date:** [July 19, 2021, 12:55pm UTC](https://www.ruby-forum.com/t/change-hash-key-based-on-hash-value/261568/2 "2021-07-19T12:55:17Z")

</div>

Not really sure what you are trying to achieve here.

Could you elaborate on what you are doing in your posted example?

---

<div class="post-metadata">

**Author:** ![bleak\_gravel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bleak\_gravel](https://www.ruby-forum.com/u/bleak_gravel)\
**Post date:** [August 29, 2024, 10:50am UTC](https://www.ruby-forum.com/t/change-hash-key-based-on-hash-value/261568/3 "2024-08-29T10:50:48Z")

</div>

> [@sunilkumarnagoore](#):
>
> how to achieve this

You have to iterate through the pairs, remove the original key/value pair and add the new one.  
Or construct a new hash based on your findings and assign it to the old hash-variable.
