ruby - Saving an Element's Index to a Variable -


i'm new ruby (and programming in general) , have been reading ton of docs, how-tos , questions try find answer problem no luck far.

i have array of integers , i'm trying save 1 of object's integers variable later delete object array. have far:

array = [3, 5, 1, 2, 6, 9] objtodel = array[3] array.delete_at(objtodel) array 

this deletes "1" in array... want delete "2" instead. know happens because having variable point array[3] points "2" instead of actual third element in array. have tried slice method no avail.

so, possible variable equal element's index instead of content? possible without turning array hash?

thanks in advance!

sure, assign index variable:

index = 3 array.delete_at(index) # => 2 array # => [3, 5, 1, 6, 9] 

you use delete method remove object directly.

object_to_delete = 2 array.delete(object_to_delete) # => 2 array # => [3, 5, 1, 6, 9] 

note deletes instances of object in array, might not want.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -