I have an array of hash that needs to be sorted by imp and I want to get the specific attribute out of it.
array_of_hash is [ { :id => "9", :subsystem => "xyz", :component => "xyz", :imp => "1", :old_serial => "55q", :current_serial => nil, :old_num => "same", :current_num => nil, :acceptable_nums => [ "asdf", "qwer", "zxcv", "poiu" ] }, { :id => "10", :subsystem => "xyz", :component => "xyz", :imp => "4", :old_serial => "56t", :current_serial => nil, :old_num => "same", :current_num => nil, :acceptable_nums => [ "asdf", "qwer", "zxcv", "poiu" ] }, { :id => "11", :subsystem => "xyz", :component => "xyz", :imp => "3", :old_serial => "57s", :current_serial => nil, :old_num => "same", :current_num => nil, :acceptable_nums => [ "asdf", "qwer", "zxcv", "poiu" ] }, { :id => "14", :subsystem => "xyz", :component => "xyz", :imp => "2", :old_serial => "58r", :current_serial => nil, :old_num => "same", :current_num => nil, :acceptable_nums => [ "asdf", "qwer", "zxcv", "poiu" ] } ] First step, sorting
array_of_hash.sort_by {|hash| hash[:imp].to_i} Then i want specific attribute
Desired output with some condition
{ :imp => "1-4", #It should be range :old_serial => "55q,56r,57s,58t", #old_serial number should be separated with comma respectively :old_num => "same", :acceptable_nums => [ "asdf", "qwer", "zxcv", "poiu" ] } I am not able to figure out how to do this.
1,2,3,4and making range1-4impandolder_serialare concatenated? What aboutold_numandacceptable_nums[1],[2], etc , and a variable should be assigned to each. That allows readers to cut-and-paste and reference the variables without having to define then