Each_value

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

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

collect and/or collect!

I think that’s what you want.

Peace,
Phillip

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.

On Dec 28, 11:11 am, Ank Ag [email protected] 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 + ‘%’}