# Create dinamically hashes from string

**URL:** <https://www.ruby-forum.com/t/create-dinamically-hashes-from-string/193996>\
**Category:** Ruby\
**Created:** [August 27, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/create-dinamically-hashes-from-string/193996 "2010-08-27T17:16:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aldoitalo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@aldoitalo](https://www.ruby-forum.com/u/aldoitalo)\
**Post date:** [August 27, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/create-dinamically-hashes-from-string/193996/1 "2010-08-27T17:16:07Z")

</div>

hi, i have a cookie with various data, i want to convert it in an hash.  
the data contain in the cookie are grouped by “;” and every group have a  
summary (which i want to convert to a name variable hash, it’s separated  
on the left items by a “-”) and various property:value (separated by  
comma):

my\_cookie =  
“my\_first\_group\_title-property1:value1,property2:value2,property3:value3,property4:value4;  
my\_second\_group\_title-blue:property1:value1,property2:value2”

def cookie\_to\_hashes(item)  
if item  
item.split(";").each do |group|  
this\_hash = {}  
group\_array = group.split("-")  
group\_array.last.split(",").each do |i|  
items = i.split(":")  
this\_hash[items.first] = items.last  
end  
p this\_hash  
end  
end

end

test = cookie\_to\_hashes(my\_cookie)

the method i have write works fine, but now i want to retrieve the  
hashes out of the method, using the names of the summary.  
in practice i want to obtain this:

test = cookie\_to\_hashes(my\_cookie)  
puts @property1  
puts @property2

how can i retrive an istance variable set with the summary string?
