Swapping key names in a hash

Hi Clem,

Clem Rock wrote:

I'm trying to change the name of a hash key. I want to take a
params[:value] and modify some of it's key names to work
better in model sql statements.

I don't think you can change the name of an existing key. It's easy to create a new hash element with the new name and then delete the old one though.

for instance I have a param hash key named "title" (params['title']) and
I want to change that key name to 'spaces.title' so it will work better
w/in a conditional statement in joined table sql call.

I'm not 100% sure that Rails will let this work in the controller since the params hash is constructed by the browser and read in the controller. Having said that, and assuming the key names you want to use are valid for Ruby hashes...

params[:spaces.title] = params[:title]
params[:title].delete

If that doesn't work then you'll probably have to construct a new hash, copy the params hash into it, and then modify it to suit your needs. There's probably a 'slicker' way to do it too. Maybe someone else will chime in with one.

Best regards,
Bill

Hi Clem,

Clem Rock wrote:

> I'm trying to change the name of a hash key. I want to take a
> params[:value] and modify some of it's key names to work
> better in model sql statements.

I don't think you can change the name of an existing key. It's easy to
create a new hash element with the new name and then delete the old one
though.

It is possible to change an existing key; that's why Hash#rehash exists.
That said, I just tried to actually accomplish it with string keys and
couldn't manage it. It does work with arrays, however:

x = { [1,2] => 3, [4,5] => 6 }

=> {[1, 2]=>3, [4, 5]=>6}

x.keys

=> [[1, 2], [4, 5]]

x.keys.first << 99

=> [1, 2, 99]

x.keys

=> [[1, 2, 99], [4, 5]]

x[[1,2]]

=> nil

x[[1,2,99]]

=> nil

x.rehash

=> {[4, 5]=>6, [1, 2, 99]=>3}

x[[1,2,99]]

=> 3

> for instance I have a param hash key named "title" (params['title']) and
> I want to change that key name to 'spaces.title' so it will work better
> w/in a conditional statement in joined table sql call.

I'm not 100% sure that Rails will let this work in the controller since the
params hash is constructed by the browser and read in the controller.
Having said that, and assuming the key names you want to use are valid for
Ruby hashes...

params[:spaces.title] = params[:title]
params[:title].delete

This has syntax problems. Try this:

params[:"spaces.title"] = params.delete(:title)

If that doesn't work then you'll probably have to construct a new hash, copy
the params hash into it, and then modify it to suit your needs. There's
probably a 'slicker' way to do it too. Maybe someone else will chime in
with one.

Best regards,
Bill

--Greg

Hi Greg,

Gregory Seidman wrote:

It is possible to change an existing key; that's why
Hash#rehash exists. That said, I just tried to actually
accomplish it with string keys and couldn't manage it.
It does work with arrays, however:

Interesting. I'd tried it before, but just with a regular hash, not a hash of arrays. Like you, I couldn't get it to work on the former. I'm trying to envision when I'd use an array as a key. What made you think to try that?

I'm not 100% sure that Rails will let this work in the controller since the
params hash is constructed by the browser and read in the controller.
Having said that, and assuming the key names you want to use are valid for
Ruby hashes...

params[:spaces.title] = params[:title]
params[:title].delete

This has syntax problems. Try this:

params[:"spaces.title"] = params.delete(:title)

I had a feeling the dot notation might cause a problem, but not the time to work through it. Thanks for the follow-through.

Best regards,
Bill