RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

Issue with removing/deleting element from Ruby Array

There is a common dangerous Ruby Array situation when removing/deleting element from Array

a = [1, 2, 3 ]
b  = a
b.delete(2)

b
#=> [1, 3]

a
#=> [1, 3] ```

Full explanation why is posted in this article

Solution

a = [1,2,3]
b = a.dup
b.delete(2)

b
# => [1, 3]

a
# => [1, 2, 3]

https://blog.eq8.eu/til/remove-element-from-ruby-array-issue.html

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in