array3 =['Maths', 'Programming', 'Physics'], ['Maths', 'Intro to comp. science', 'Programming'], ['English', 'Intro to comp. science', 'Physics']
course = 'Programming'
index = []
array3.find_index do |i|
if array3.include?(course) == true
index << i
end
end
i created an array (array3) that contains the respective elements and i want to add the elements of array3 which hold the condition true but after executing the code i get a blank array like "[[], [], []]" how can i fix this issue?
x == trueis almost always better written asx.index. Thefind_indexname is misleading. Additionally the argument to theindexblock is notias in index, but the value. It returns the index.array3like that is problematic for two reasons. 1. It should bearray3 = [ [ ... ], ... ]where it's clear that's a 3 part array. 2. The name is terrible. Call itcoursesor something meaningful.course_listsis a fitting name. This also helps the reader to understand that they're working with a nested arrays.