Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ruby delete command misbehaving?

Manipulating an array of integers is returning unexpected results with ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin13] :

a = [1, 126, 158, 201, 102]
=> [1, 126, 158, 201, 102]
b = 102
=> 102
a.delete(b)
=> 102

many slight variations of syntax always lead to the same result.

is the syntax mistaken, could there be some higher command that negates this command or worse, is some corruption issue at hand? How can this be ascerted?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

From the Array#delete docs:

When no block is given, removes from self each element ele such that
ele == obj; returns the last deleted element…

In your case b is equal to 102, hence it prints its value after defining the variable. Then a.delete(b) corroborates that there’s an element in a equals to b so it deletes it and returns the value of b.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading