1

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??

1
  • You could also use delete_at but Sergio Tulentsev answer is great. Commented Oct 15, 2012 at 4:46

1 Answer 1

4

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"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.