drop method

The drop method takes an enumerable collection, and returns everything except the first n elements.

Number List

@numbers = [1, 0, 3, 2, 5, 4, 7, 6, 9, 8]

# skipping the first two numbers:
@numbers.drop(2)
    #=> [3, 2, 5, 4, 7, 6, 9, 8]

# skipping no numbers:
@numbers.drop(0)
    #=> [1, 0, 3, 2, 5, 4, 7, 6, 9, 8]

Pet Inventory

# skipping the first two pets:
@inventory.drop(2).map(&:name)
    #=> ["fish", "scorpion", "beetle", "monkey", "rock"]

# skipping no pets:
@inventory.drop(0).map(&:name)
    #=> ["dog", "cat", "fish", "scorpion", "beetle", "monkey", "rock"]

Pokey Things

# skipping first pokey thing:
@pokey_things.seek(0)
@pokey_things.drop(1).map(&:chomp)
    #=> ["pole", "knife", "cactus holding poles with knives attached"]

# skipping no pokey things:
@pokey_things.seek(0)
@pokey_things.drop(0).map(&:chomp)
    #=> ["cactus", "pole", "knife", "cactus holding poles with knives attached"]

Heroku Log File

# skipping first 3 heroku requests:
@requests.drop(3).map(&:id)
    #=> [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]

# skipping no heroku requests:
@requests.drop(0).map(&:id)
    #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]

Leave a Reply

Your email address will not be published. Required fields are marked *