Reputation: 71
I have an hash consisting of:
flow = ["a", "b", "c"]
h = [{"case_id"=>1, "a"=>{"x"=>"text", "option"=>"..." },"b"=>{"report"=>"text", "option"=>"..." } ,"c"=>{"y"=>"text", "option"=>"..." }},{"case_id"=>2, "a"=>{"x"=>"text", "option"=>"..." },"b"=>{"report"=>"text", "option"=>"..." } ,"c"=>{"y"=>"text", "option"=>"..." }}]
@case = 0
@report = ""
flow.each do |step|
if h[@case][step]['report']
@report = h[@case][step]['report']
end
end
the console gives me the following error:
NoMethodError in MainController#index
undefined method `[]' for nil:NilClass
Why?! In a previous program, but using ruby 187, gave no problem. But now, with ruby 193, gives this error.
Upvotes: 0
Views: 1442
Reputation: 9146
flow = ["a", "b", "c"]
h = { "a"=>{"x"=>"text", "option"=>"..." },"b"=>{"report"=>"text", "option"=>"..." } ,"c"=>{"y"=>"text", "option"=>"..." }}
flow.each do |f|
if h[f]['report']
#something
end
end
Ruby has some great iteration helper provided by enumeration class
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-each
Upvotes: 1
Reputation: 47532
You will not get error till either h
or h[a]
is nil
. Please update your question properly.
Upvotes: 0