Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Javier Pacheco3,418 Points
.slice removes items from Array.
In video he states that the .slice(0,3) method does not remove items from original Array but the teachers notes state otherwise.
Krahr ChaudhuryCourses Plus Student 139 Points
slice() does NOT remove elements from the original array, it returns a copy of the original array with the elements removed accordingly.
slice!() on the other hand directly modifies the original array.
array1 = [1,2,3,4,5] array2 = [1,2,3,4,5] def normal_slice(arr) arr.slice(0,3) arr end def dangerous_slice(arr) arr.slice!(0,3) arr end array1 = normal_slice(array1) #[1,2,3,4,5] array2 = dangerous_slice(array2) #[4,5]