ruby - Best approach for deleting when using Array.combination()? -


i want compare every object in lectures each other , if some_condition true, second object has deleted:

todelete=[] lectures.combination(2).each |first, second|   if (some_condition)    todelete << second   end end todelete.uniq! lectures=lectures-todelete 

i got weird errors while trying delete inside .each loop, came approach.

is there more efficient way this?

edit after first comments:

i wanted keep source code free of unnecessary things, ask: elements of lectures array hashes containing data of different university lectures, name, room,the calendar weeks in taught , begin , end time.

i parse timetables of student groups data, because lectures held in more 1 student group , these differ in weeks taught, compare them each other. if compared ones differ in values, add values second object first object , delete second object. that's why.

the errors when deleting while in .each-loop: when using rails hash.diff method, got "cannot convert symbol integer". turns out there integer value of 16 in array, although tested before loop there hashes in array...

debugging hard if have 9000 hashes.

edit: sample data:

lectures = [ {:day=>0, :weeks=>[11, 12, 13, 14], :begin=>"07:30", :end=>"09:30", :rooms=>["li201", "g221"], :name=>"testsubject1", :kind=>"vw", :lecturers=>["waldm"], :tut_groups=>["11inm"]},  {:day=>0, :weeks=>[11, 12, 13, 14], :begin=>"07:30", :end=>"09:30", :rooms=>["li201", "g221"], :name=>"testsubject1", :kind=>"vw", :lecturers=>["waldm"], :tut_groups=>["11inm"]} ] 

you mean this?

cleaned_lectures = lectures.combination(2).reject{|first, second| some_condition} 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -