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 Ruby Collections Ruby Arrays Removing Items From Arrays

Cannot get .drop() to extract two items from grocery list.

Based on the removing items from Arrays video, the following code should have extracted two items from a x-item array, but it apparently doesn't:

[23] pry(main)> a
=> ["milk", "eggs", "carrots"]
[24] pry(main)> d2 = a.drop(2)
=> ["carrots"]
[25] pry(main)> a
=> ["milk", "eggs", "carrots"]
[26] pry(main)> 

Being quite familar with enumerables like Array having drop() and similar methods like take having to be consistent within Ruby for them to be relied upon for critical applications once the Enumerable class is mixin into them, I'm confused why I'm experiences this problem with Ruby 2.1.3

3 Answers

Jamie Barton
Jamie Barton
14,498 Points

From the code above it looks to be doing what it is meant to.

It returns the array with the first n elements removed. It doesn't reassign the variable with the returned values.

I'd recommend assigning a new variable like b = a.drop(2) to see the results in a new array.

You could of course do a = a.drop(2) to change the value of a.

Hope this helps!

It seems I misunderstood the capabilities of take with drop despite using them both frequently in the past. It seems I confused myself as a result of following along the video with pry with a different array.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

d2 now equals ["carrots"] and a was not modified at any point (there was no assignment = used and there was no 'potentially unsafe' method used with ! at the end). The drop method's definition: "Drops first n elements from ary and returns the rest of the elements in an array."

Maciej Czuchnowski, Jamie Barton:

I'm actually getting odd behavior from the method I never seen before in previous versions of Ruby:

[3] pry(main)> l = [ "eggs", "bread", "ice cream", "pie", "potatoes" ]
=> ["eggs", "bread", "ice cream", "pie", "potatoes"]
[4] pry(main)> lb = l
=> ["eggs", "bread", "ice cream", "pie", "potatoes"]
[5] pry(main)> l.slice(0, 3)
=> ["eggs", "bread", "ice cream"]
[6] pry(main)> l.drop(2)
=> ["ice cream", "pie", "potatoes"]
[7] pry(main)> a = l.drop(2)
=> ["ice cream", "pie", "potatoes"]
[8] pry(main)> a
=> ["ice cream", "pie", "potatoes"]
[9] pry(main)> l
=> ["eggs", "bread", "ice cream", "pie", "potatoes"]
[10] pry(main)> a = l.drop(1)
=> ["bread", "ice cream", "pie", "potatoes"]

Knowing Jason Seifer is usually cutting-edge regarding recent developments with Ruby, perhaps he can explain how I'm getting the input above from (drop). I doubt my copy of Ruby version 2.1.3 is bugged though I updated to it this morning from 2.1.2

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

What exactly is the unexpected behavior (I'm not sure where to look for it)?

Doh, I think I made the same mistake of using drop instead of take. I apologize; it's been a long week. :)