Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Ruby Collections Ruby Arrays Removing Items From Arrays

Kody Ellis
Kody Ellis
2,098 Points

Why can't I delete more than 1 thing in a Ruby array using .delete_at?

Why can't you pass more than 1 argument using the delete_at method???Even the official documentation is unclear on this.

Actual delete method will delete an array item based on actual name

grocery_list = ["0apple", "1apple", "2apple", "3apple", "4apple", "5apple",]

Delete_at method deletes an item based on its index number

#prints out: ["1apple", "2apple", "3apple", "4apple", "5apple",]
grocery_list.unshift "0apple"

However, when I tell it to delete more than 1 thing, it says error

#prints out: ["0apple", "2apple", "3apple", "4apple", "5apple",]

I should get this

["0apple", "3apple", "4apple", "5apple",]

But, I get an error message like this:

rb:11 :in `delete_at': wrong number of arguments (2 for 1) (ArgumentError)
from C:/Users/mildr_000/rubyhotspot2.rb:11:in ' main '

If delete_at doesn't work, how else can I delete multiple items in the array based on index number?(without using a loop)

1 Answer

dan parker
dan parker
9,843 Points

Delete_at(index) Deletes the element at the specified index, returning that element, or nil if the index is out of range. From the Docs.

It accepts only one argument. You can only delete at one index, the return is the deleted item, and the original object is modified.

Try slice, take, take_while, drop, delete_if there is an option depending on you goal.