if unit_id_hash.nil?
unit_id_hash = {}
s_code << {unid_id => unit_id_hash}
end
# this lookup could be done via a constant Hash as well
key = case code
when 3,4
:code_a
when 8,9
:code_b
end
unit_id_hash[key] << code if key
end
But frankly, your data structure looks suspicious to me. For example:
why is session[:code] an Array containing of Hashes with only one key
value pair? Why don't you make it a Hash where indexed by code?
Kind regards
robert
···
On Sun, Oct 9, 2011 at 11:28 PM, John Merlino <stoicism1@aol.com> wrote:
Hey all,
I basically have some nested hashes stored in the session variable:
def set_code(unit_id, code)
if session\[:code\]\.select \{|h| h\[unit\_id\]\}\.empty?
session\[:code\] << \{ unit\_id => \{\}\}
end
case code
when 3,4
session\[:code\]\.detect \{|h| h\[unit\_id\]\}\[unit\_id\]\[:code\_a\] ||= \[\]
session\[:code\]\.detect \{|h| h\[unit\_id\]\}\[unit\_id\]\[:code\_a\] << code
when 8,9
session\[:code\]\.detect \{|h| h\[unit\_id\]\}\[unit\_id\]\[:code\_b\] ||= \[\]
session\[:code\]\.detect \{|h| h\[unit\_id\]\}\[unit\_id\]\[:code\_b\] << code
end
end
From the code above, you can see four lines of repetitive code. The
problem is if I did this: