0

This is the array:

array = [ 1, 2, 3, [4, 5, 6] ]

Can I use "delete_at" method to delete the "5"?

array.delete_at[x] method

What would the correct syntax be?

3
  • array[3].delete_at(1)? Commented Jul 8, 2016 at 21:54
  • 2
    Do you mean you want to delete the element array[3][1] (whatever its value) or do you want to delete the 5, not knowing where it is in the array (and assuming the array has a particular structure)? Commented Jul 9, 2016 at 3:47
  • Please show what you want the result to be. Commented Jul 9, 2016 at 16:59

2 Answers 2

3

Your 'array' has only 4 elements. If it's subarray you probably should do something like that

array[3].delete_at(1)

to delete the second element of subarray that's a fourth element of 'array' array.

Sign up to request clarification or add additional context in comments.

Comments

-2

Welcome to Stack Overflow!

This one is longer and less efficient but it allows you to select the item to be deleted by value instead of by position (array index). That's useful when you don't know the position.

array.map {|x| x.delete(5) if x.instance_of?(Array); x}

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.