Enumerating Enumerable marches on!
This is the seventh article in my series in which I try to do a better job of documenting Ruby’s Enumerable
module than Ruby-Doc.org does. If you’ve missed the other articles in the series, they’re listed below:
In this installment, I cover a method added in Ruby 1.9: drop
.
Enumerable#drop Quick Summary
In the simplest possible terms | Given a collection and a number n, create an array made of the items of the collection with the first n items removed. |
---|---|
Ruby version | 1.9 only |
Expects | The number of elements to remove from the start of the collection. |
Returns | An array made up of the remaining items, if there are any./td> |
RubyDoc.org’s entry | Enumerable#drop |
Enumerable#drop and Arrays
With an array, drop
takes a number n as an argument and returns an array created by removing the first n elements of the array. The resulting array is made up of the remaining elements.
# These are the favourite bands of RubyFringe organizer # Meghan Katleen Millard's, according to her Facebook profile meghans_fave_bands = ["Afghan Whigs", "Bjork", "Charles Mingus", "Deerhunter", "Electrelane", "Francois Hardy", "Godspeed You Black Emperor!"] => ["Afghan Whigs", "Bjork", "Charles Mingus", "Deerhunter", "Electrelane", "Francois Hardy", "Godspeed You Black Emperor!"] # Let's lose the first 5 meghans_fave_bands.drop 5 => ["Francois Hardy", "Godspeed You Black Emperor!"] # The original array is not affected meghans_fave_bands => ["Afghan Whigs", "Bjork", "Charles Mingus", "Deerhunter", "Electrelane", "Francois Hardy", "Godspeed You Black Emperor!"]
Enumerable#drop and Hashes
With an hash, drop
takes a number n as an argument and returns an array created by removing the first n elements of the array (drop
is only in Ruby 1.9 and later, where hashes keep the order in which they were defined, so its results are predictable). The resulting array is made up of the remaining elements, with each element converted into a two-element array where element 0 is the key and element 1 is the corresponding value.
# Here's a hash of the first five U.S. states # (in terms of alphabetical order) and their capitals. # This example is in Ruby 1.9, which means that # the hash keys will stay in the order in which # they were defined. states_and_capitals = {"Alabama" => "Montgomery", \ "Alaska" => "Juneau", \ "Arizona" => "Phoenix", \ "Arkansas" => "Little Rock", \ "California" => "Sacramento"} => {"Alabama"=>"Montgomery", "Alaska"=>"Juneau", "Arizona"=>"Phoenix", "Arkansas"=>"Little Rock", "California"=>"Sacramento"} # Let's remove the first 3 states_and_capitals.drop 3 => [["Arkansas", "Little Rock"], ["California", "Sacramento"]] # The original hash is not affected states_and_capitals => {"Alabama"=>"Montgomery", "Alaska"=>"Juneau", "Arizona"=>"Phoenix", "Arkansas"=>"Little Rock", "California"=>"Sacramento"}
Enumerable#take: Enumerable#drop’s Evil Twin
I’ll cover take
in detail in a later installment, but for now, an example should suffice:
meghans_fave_bands = ["Afghan Whigs", "Bjork", "Charles Mingus", "Deerhunter", "Electrelane", "Francois Hardy", "Godspeed You Black Emperor!"] => ["Afghan Whigs", "Bjork", "Charles Mingus", "Deerhunter", "Electrelane", "Francois Hardy", "Godspeed You Black Emperor!"] # drop(n) removes the first n elements meghans_fave_bands.drop(4) => ["Electrelane", "Francois Hardy", "Godspeed You Black Emperor!"] meghans_fave_bands.take(4) => ["Afghan Whigs", "Bjork", "Charles Mingus", "Deerhunter"]
6 replies on “Enumerating Enumerable: Enumerable#drop”
Might want to add links to all the articles in the series to the first article. Thanks for doing these, they are a great help!
[…] drop […]
[…] drop […]
[…] drop […]
[…] niceties), but I have no excuse for blowing this. I’ve actually written a whole series of articles on the power of Ruby’s Enumerable module, including the select method (and what I think was […]
[…] acuerdo a la globalnerdy.com/2008/07/10/… , caída de ruby 1.9, en lugar de ruby […]