ruby on rails - Return string from multiple array items -
i have multiple arrays have code string items in them. need match code given string , return class name matched array.
might better if show i've got. below arrays , underneath string need return if given string matches item within array. lets send string of '329' should return 'ss4' string:
['392', '227', '179', '176'] = 'ss1' ['389', '386'] = 'ss2' ['371', '338', '335'] = 'ss3' ['368', '350', '332', '329', '323', '185', '182'] = 'ss4'
i need know best approach this. create helper method , have array each code block , check each array see if given string code contained , return string, ss1 or ss4. idea?
the efficient approach generate translator hash once can perform lookup super fast:
codes = { ss1: ['392', '227', '179', '176'], ss2: ['389', '386'], ss3: ['371', '338', '335'], ss4: ['368', '350', '332', '329', '323', '185', '182'] } translator = codes.each_with_object({}){|(s, a), m| a.each{|n| m[n] = s.to_s}}
now can do:
translator['329'] => "ss4" translator['389'] => "ss2"