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:
unit_hash = session[:code].detect {|h| h[unit_id]}[unit_id]
case code
when 3,4
unit_hash[:code_a] ||= []
unit_hash[:code_a] << code
when 8,9
unit_hash[:code_b] ||= []
unit_hash[:code_b] << code
end
Then the appended code will not be stored in the session.
Any way to refactor this so I dont have 4 repetitive lines of code?
Thanks for response