Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKody Ellis
2,098 PointsWhy 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",]
grocery_list.delete("0apple")
Delete_at method deletes an item based on its index number
#prints out: ["1apple", "2apple", "3apple", "4apple", "5apple",]
grocery_list.unshift "0apple"
grocery_list.delete_at(1)
However, when I tell it to delete more than 1 thing, it says error
#prints out: ["0apple", "2apple", "3apple", "4apple", "5apple",]
grocery_list.insert(1,"1apple")
grocery_list.delete_at(1,2)
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
9,843 PointsDelete_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.