-1

Trying to delete from this array:

array = ["Dog", "Cat", "John Cena"]

But those won't work:

array.delete(1)
array.delete([1])

Any help is appreciated.

2
  • 2
    "those won't work" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Jan 6, 2019 at 20:11
  • 2
    Also, another hint: it would be very helpful if you could explain what, precisely is unclear to you about the documentation of Array#delete. That way, the Ruby developers can improve the documentation so that future developers don't stumble over the same problems you did. Make the world a better place! Jan 6, 2019 at 20:12

3 Answers 3

5

It's delete_at you needed:

array = ["Dog", "Cat", "John Cena"]
#=> ["Dog", "Cat", "John Cena"]
array.delete_at(1)
#=> "Cat"
array
#=> ["Dog", "John Cena"]

Please check the doc more often, you'll probably find the thing you needed most of the times.

1

Alternatively:

array = ["Dog", "Cat", "John Cena"]
array[1, 1] = []
array # => ["Dog", "John Cena"]
-1

As stated before you should be using Ruby Docs for help like this Ruby has amazing documentation and is a shame to waste the wealth of knowledge here.

https://ruby-doc.org/core-2.1.0/Array.html#method-i-delete_at

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.