Based on my problem of trying to merge params into another hash, and
the very interesting question raised by Mark W., maybe the
problem is related how you are keying your hash. I was trying to
merge params into a hash keyed with symbols, and it didn’t work. But
when I changed the keys to strings, it did work. So instead of
This is a standard Ruby-ism :string and ‘string’ are not the same
thing. The leading colon makes it into a Symbol, which is converted
into a unique integer behind the scenes. This makes it useful for
hashes, because you can use this value directly for the hash instead
of having to process it into some kind of key the way you would if it
were a String.
I think the convention is that the top-level things in params (e.g.
the model name) use Symbols and the lower-level stuff (as in the form
arguments) are indexed with strings. It doesn’t matter which you use,
as long as you are consistent, for example you could use Symbols in
your session hash.
String has to_sym and Symbol has to_s so you can get from one to the
other easily.
I feel your pain too guys, becuase I went through this one myself a
couple of months ago, but I was parsing XML. This is why XmlSimple has
the KeyToSymbol argument, as I discovered after running in circles for
a while.
I’m not sure your statement about it not mattering which you use is
entirely accurate. I was reading an article about this (can’t
remember where), and strings can have a significantly bigger impact
on memory usage because each one is a completely separate identifier
and therefore has its own memory space. The rule of thumb that I
I think the convention is that the top-level things in params (e.g.
the model name) use Symbols and the lower-level stuff (as in the form
arguments) are indexed with strings. It doesn’t matter which you use,
as long as you are consistent, for example you could use Symbols in
your session hash.
I’m not sure your statement about it not mattering which you use is
entirely accurate. I was reading an article about this (can’t
remember where), and strings can have a significantly bigger impact
on memory usage because each one is a completely separate identifier
and therefore has its own memory space. The rule of thumb that I
read (and have decided to adopt) is: if you care about the content of
the thing, use a string; if it’s just an identifier, use a symbol.
I’m going through my entire application changing all strings to
symbols where I can, and it’s turning out to be most places.
As for convention, so far all of the Rails code I’ve looked at makes
almost exclusive use of symbols. In fact, now that I think about it,
I can’t recall seeing the use of a string. But I haven’t scoured
every line of Rails either. The easier use of symbols is why they
created HashWithIndifferentAccess, which is what params is.