From: David Masover Date: 2010-08-26T12:29:35+09:00 Subject: Re: Some odd ball optimization tricks.... On Wednesday, August 25, 2010 06:51:58 pm John Carter wrote: > # Similar to the "autosequence" facility in SQL. > # Useful for replacing a complex key with a very simply POD proxy object. > file_sequence = 0 > file_index = Hash.new{|hash,key| file_sequence+=1;hash[key] = > file_sequence} > > > p file_index["foo"] > p file_index["foo"] > p file_index["bah"] > p file_index["foo"] > p file_index["bah"] > pp file_index I guess I can think of a few rare instances this might be useful, but I've never had the keys be the bottleneck, or re-used keys enough in a script for this to matter. Still, interesting. > # Similar to "to_sym", but can cope with spaces and weird characters... ...what? irb> 'foo bar'.to_sym => :"foo bar" > "a@$bc☃!d\t".to_sym => :"a@$bc☃!d\t" Worst thing that happens is 1.8 turns my beautiful Unicode into ugly hex escapes when pretty-printing, since it's all just a binary string to 1.8. But if I print it straight out with puts, I get the original Unicode stuff back, and 1.9.1 handles this gracefully. I mean, I've got a friggin' SNOWMAN in there. Just what characters have you discovered that you can't make a string out of? And, as it suggests, you can use :"foo" or :'foo' as shorthand for the above. > # So if tom1, tom2, tom3... go out of scope they can be garbage > collected... Same is true of any string you call to_sym on. > you can test for > # equality with .object_id == .object_id! I don't know if String is smart enough to do that (I'd hope so), but Symbol certainly would be. So what does this solve over just using symbols, other than the fact that you could manually prune the one_true_string hash?