Tormyst
Tormyst

Reputation: 187

Symbol not equal to Symbol

I have a symbol that I pass into a function call like this in my controller:

set(:value, 1)

The function is in a helper function that more or less works like this

def set(where, what)
  session[:a][where] = what
end
def get(where)
  return session[:a][where]
end

I use this same symbol in my view in another function that should grab the same value:

get(:value)

And by debugging I have found that I end up checking 2 values in my hash table. My get looks to work fine, but when I try and call the get from my view, my hash tries to find a value, but with single quotes around it. I have tried a bunch of stuff to see what is going on, changing the values to be hard coded into the function is not helping, however, replacing the symbol with a string is.

Upvotes: 0

Views: 267

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230541

It looks like your symbols are stringified (during session serialization). To avoid that, stringify them preemptively!

def set(where, what)
  session[:a][where.to_s] = what
end
def get(where)
  return session[:a][where.to_s]
end

Upvotes: 1

Related Questions