I recently began playing with mod_ruby, eruby and cgi.rb, which are all
wonderful things.
I would find it useful to be able to retrieve cookie values by indexing on
the name of the cookie, but I couldn't find a way to do this.
If there is such a method, can someone tell me what it is?
If not, here's a small addition I made to cgi.rb that provides such a
mechanism.
It basically does precisely what CGI#cookies does, but returns a hash,
instead of an array.
Feel free to recode it, rename it, in fact do anything you like with it.
Also, what I really wanted to do was to use eval to generate a variable
for each of the values stored in a cookie. However, because of the
SAFE level at which the code is running, eval is forbidden (I'm not sure
whether this is mod_ruby, eruby or cgi.rb).
Is it possible to override this? I'm guessing not, since that would
bypass the security, but I thought I'd ask, just in case.
-------------------------------------------------
class CGI
def cookies_hash(name)
array = cookies[name]
hash = Hash.new()
array.each do |c|
nameValue = c.split('=')
hash[nameValue[0]] = nameValue[1]
end
return hash
end
end