Xitcod13
Xitcod13

Reputation: 6079

removing array elements by indexes from an array ruby

I have an array of words like so

["jo","jibber","hail","noobs","smirk","awkland"] 

and a seperate array with indexes

[0,3,5]

I want to remove all the elements from the previous array at those indexes. I was thinking i could use reject but im not sure exactly how i would do it. Also once i remove one element wont all the other indexes would have to change. Is there an easy way of doing this??

Upvotes: 1

Views: 154

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

You can use reject and with_index

arr = ["jo", "jibber", "hail", "noobs", "smirk", "awkland"] 

indexes = [0, 3, 5]

arr.reject.with_index {|_, idx| indexes.include?(idx)} # => ["jibber", "hail", "smirk"]

Upvotes: 4

Related Questions