Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

How do I know which method would change the call object itself if there is no "!" required. It seems confused

like the function keep_if will change the call object itself but without symbol "!". It seems not consistency.

2 Answers

Dylan Shine
Dylan Shine
17,565 Points

In the Ruby Code Docs (http://www.ruby-doc.org/core-2.1.3/Array.html#method-i-keep_if) there are methods that are destructive and non destructive. Since .keep_if is a destructive method, it will modify the array regardless of having a ! sign. To check if a method is destructive or now, look at whether the method you're using also has a ! version listed in the Ruby Core Docs.

so that means, there is no ! sign version for destructive methods.

Dylan Shine
Dylan Shine
17,565 Points

Correct, in order to not modify your array chances are you're going to need to duplicate it before you operate on it.