# Can store array or hash in cookies?

**URL:** <https://www.ruby-forum.com/t/can-store-array-or-hash-in-cookies/51819>\
**Category:** Rails\
**Created:** [January 7, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/can-store-array-or-hash-in-cookies/51819 "2006-01-07T17:21:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![szczepiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@szczepiq](https://www.ruby-forum.com/u/szczepiq)\
**Post date:** [January 7, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/can-store-array-or-hash-in-cookies/51819/1 "2006-01-07T17:21:15Z")

</div>

Hi

According to rdoc: value - the cookie’s value or list of values (as an  
array).

So, I can use arrays, briliant.  
But… It does not work (at least on webrick)

I wrote a controller:

def check\_cookies\_first  
cookies[:chef] =  
{:value =\> [“first”, “second”], :expires =\> Time.local  
(2020)}

```
    render_text ":)"

```

end

def check\_cookies\_second  
begin  
render\_text "cookies class is: " + cookies[:chef].class.to\_s

- "  
value: " + cookies[:chef]  
rescue Exception =\> e  
render\_text "Why this don’t work? exception: " + e.to\_s  
end  
end

Started server:

get check\_cookies\_first —\> “:)”  
get check\_cookies\_second —\> “cookies class is: String value: first”

What is wrong? Is it a webrick issue?

Thanks a lot,  
Szczepan

---

<div class="post-metadata">

**Author:** ![szczepiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@szczepiq](https://www.ruby-forum.com/u/szczepiq)\
**Post date:** [January 8, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/can-store-array-or-hash-in-cookies/51819/2 "2006-01-08T16:27:28Z")

</div>

if you add this method,

```
 def cookie(name)
   @cookies[name.to_s].value if @cookies[name.to_s] && 

```

@cookies[name.to\_s].respond\_to?(:value)  
end

to the CookieJar class in the cookies.rb file  
you would be able to access the entire cookie array.

cookies.cookie(:chef)

=\> [“first”, “second”]

---

<div class="post-metadata">

**Author:** ![szczepiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@szczepiq](https://www.ruby-forum.com/u/szczepiq)\
**Post date:** [February 19, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/can-store-array-or-hash-in-cookies/51819/3 "2006-02-19T16:59:04Z")

</div>

Surely there is an other way?? If not is this then not just a bug?

Lou V. wrote:

> if you add this method,
> 
> ```
> def cookie(name)
> @cookies[name.to_s].value if @cookies[name.to_s] && 
> 
> ```
> 
> @cookies[name.to\_s].respond\_to?(:value)  
> end
> 
> to the CookieJar class in the cookies.rb file  
> you would be able to access the entire cookie array.
> 
> cookies.cookie(:chef)
> 
> =\> [“first”, “second”]
