first method

The first method takes an enumerable collection and optional number of elements. If no element count is given, it returns the first element in the collection. If a number is given, it returns that many elements from the beginning of the collection. Careful! This can be tricky, since it returns different types of objects (scalars or arrays) depending on the paramenters given.

Number List

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

# first number:
    #=> 1

# first three numbers:
    #=> [1, 0, 3]

Pet Inventory

# first pet on the list:
    #=> "dog"

# first 3 pets on the list:
    #=> ["dog", "cat", "fish"]

Pokey Things

# first pokey thing:
    #=> "cactus"

# first three pokey things:
    #=> ["cactus", "pole", "knife"]

Heroku Log File

# first heroku request:
    #=> 1

# first three requests:
    #=> [1, 2, 3]

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>