From: David MacMahon Date: 2013-02-07T09:54:53+09:00 Subject: [ruby-core:51956] Re: [ruby-trunk - Feature #7792] Make symbols and strings the same thing Hi, Rodrigo, FWIW, I sympathize with your symbols-vs-strings as keys frustration, but I think it's not so trivial to have the best of both worlds (flexibility and performance). Here is a simplification of your example: require 'redis' cache = Redis.new value = :value key = 'key' v1 = cache[key] || (cache[key] = value) v2 = cache[key] || (cache[key] = value) p v1 # :value or "value" p v2 # always "value" p cache[key] == value # always false IMHO, the crux of the problem here is that Redis converts all Symbols to Strings, but assigning "v1 = cache[key] = value" does not store a String in v1 if value is a Symbol. This could be addressed by doing: v1 = cache[key] || (cache[key] = value; cache[key]) which will store a String in v1 if value is a Symbol even if key does not yet exist in cache. Yes, it is an extra cache fetch. The same kind of thing happens if value is a Fixnum. Surely you wouldn't want Fixnums and Strings to be the same thing! :-) Thanks for starting this fascinating thread, Dave On Feb 6, 2013, at 3:45 PM, Rodrigo Rosenfeld Rosas wrote: > Ok, you missed the point. Let me show you a complete example as I think it will help you understanding my concerns (I thought I made myself clear in the ticket description, but I hope this code example will help you understand what I meant): > > require 'redis' > require 'json' > require 'sequel' > > cache = Redis.new > users = cache['users'] || begin > db = Sequel.connect('postgres://user:password@localhost/mydb') > cache['users'] = db[:users].select(:id, :name).map{|r| id: r[:id], name: r[:name]} # or just select(:id, :name).all > end > > p users.first[:id] > > > What will be the output of the code above? > > Exactly! It depends! If the results were cached it will print "nil", otherwise it will print "1" (or whatever the first id is). > > This is the problem that symbols cause because they don't behave like strings. > > Best, > Rodrigo.