# Each\_value

**URL:** <https://www.ruby-forum.com/t/each-value/124442>\
**Category:** Rails\
**Created:** [December 28, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/each-value/124442 "2007-12-28T18:11:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 28, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/each-value/124442/1 "2007-12-28T18:11:58Z")

</div>

Hello,

I have a params hash which is called  
params[:person]

my people database has fields  
first\_name  
last\_name  
etc…

When the web form is submitted for creation of a person, the  
params[:person] contains the value of all the person attributes.  
for e.g  
params[:person]{“first\_name”=\>“John”,“last\_name”=\>“Anderson”}

I WANT TO MODIFY IT AS  
params[:person]{“first\_name”=\>"%John%",“last\_name”=\>"%Anderson%"}

NOW THE PROBLEM IS THAT I NEED TO ADD A % SIGN AT THE BEGINNING AND END  
OF EACH VALUE IN HASH. SO I HAVE USED

params[:person].each\_value{|value| value="%"+value+"%"}

but the above code does not work…The params hash remains UNCHANGED.  
Can anyone please tell me what do i need to do…and where am i going  
wrong.

Thank you  
Regards,  
Ank

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 28, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/each-value/124442/2 "2007-12-28T18:46:48Z")

</div>

On Dec 28, 2007, at 11:11 AM, Ank Ag wrote:

> OF EACH VALUE IN HASH. SO I HAVE USED
> 
> params[:person].each\_value{|value| value=“%”+value+“%”}
> 
> but the above code does not work…The params hash remains UNCHANGED.  
> Can anyone please tell me what do i need to do…and where am i going  
> wrong.

Take a look at

[http://www.ruby-doc.org/core/classes/Array.html](http://www.ruby-doc.org/core/classes/Array.html)

collect and/or collect!

I think that’s what you want.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 28, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/each-value/124442/3 "2007-12-28T18:59:46Z")

</div>

Anthony wrote:

> > Posted viahttp://www.ruby-forum.com/.  
> > The params hash remains unchanged because the ‘value’ variable is  
> > local to that loop, so it doesn’t change anything outside the loop  
> > (such as the params). It changes the ‘value’ variable, but not the  
> > params variable. In order to change the params, you need to directly  
> > reference them in the loop, something like
> 
> params[:person].each{|k,v| params[:person][k] = ‘%’ + v + ‘%’}

Thanks a lot Anthony…Its working now.

---

<div class="post-metadata">

**Author:** ![ankit2584](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/51bf81/32.png) [@ankit2584](https://www.ruby-forum.com/u/ankit2584)\
**Post date:** [December 28, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/each-value/124442/4 "2007-12-28T18:47:58Z")

</div>

On Dec 28, 11:11 am, Ank Ag [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When the web form is submitted for creation of a person, the  
> params[:person].each\_value{|value| value=“%”+value+“%”}
> 
> but the above code does not work…The params hash remains UNCHANGED.  
> Can anyone please tell me what do i need to do…and where am i going  
> wrong.
> 
> ## Thank you Regards, Ank
> 
> Posted viahttp://www.ruby-forum.com/.  
> The params hash remains unchanged because the ‘value’ variable is  
> local to that loop, so it doesn’t change anything outside the loop  
> (such as the params). It changes the ‘value’ variable, but not the  
> params variable. In order to change the params, you need to directly  
> reference them in the loop, something like

params[:person].each{|k,v| params[:person][k] = ‘%’ + v + ‘%’}
