What is a good strategy for symbols that might have spaces in them?
I want to turn usernames into symbols before they are saved in my开发者_JS百科 database, this seems logical since that username will be referred to often and we want to conserve on memory. But what's a good option to substitute into the string in place of any spaces or other illegal characters? Or should it be kept a string to avoid the trouble?
Keep it as a string, avoid the trouble.
Worrying about this kind of thing is what's called Premature Optimization. And besides, a username is unlikely to be shared between users, so you wouldn't be conserving much memory regardless.
If you absolutely must have spaces in your symbols, Ruby will let you do that:
foo = :"symbol with spaces" # => :"symbol with spaces"
foo.inspect # => ":\"symbol with spaces\""
精彩评论