Should I use quotes or colons for session keys in Ruby on Rails?

ruby-on-rails

Solution

In a standard Ruby Hash they're not the same (string vs symbol)

However, the rails `SessionHash` subclass calls `to_s` on the keys before storing them, so all keys are stored as strings (even if you specify a symbol):

class SessionHash < Hash

  def [](key)
    load_for_read!
    super(key.to_s)
  end

  def []=(key, value)
    load_for_write!
    super(key.to_s, value)
  end

That's why `session[:test]` and `session["test"]` will return the same value in rails.

Problem

Currently I have a session variable that returns the same value when I use both `session["test"]` or `session[:test]`. Are these two the same? Is it better to use one over the other? Thanks

Original source