Class: NBA::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/nba/collection.rb

Overview

Represents a collection of objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements = []) ⇒ NBA::Collection

Initializes a new Collection

Examples:

collection = NBA::Collection.new([player1, player2])

Parameters:

  • elements (Array) (defaults to: [])

    the elements in the collection



71
72
73
# File 'lib/nba/collection.rb', line 71

def initialize(elements = [])
  @elements = elements
end

Instance Attribute Details

#elementsArray (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the elements in the collection

Returns:

  • (Array)

    the elements



62
63
64
# File 'lib/nba/collection.rb', line 62

def elements
  @elements
end

Instance Method Details

#each {|element| ... } ⇒ Enumerator, self

Iterates over the elements in the collection

Examples:

collection.each { |element| puts element }

Yields:

  • (element)

    yields each element to the block

Returns:

  • (Enumerator, self)

    an enumerator or self



82
83
84
85
86
87
# File 'lib/nba/collection.rb', line 82

def each(&block)
  return enum_for unless block

  elements.each(&block)
  self
end

#empty?Boolean

Returns true if the collection has no elements

Examples:

collection.empty? #=> false

Returns:

  • (Boolean)

    true if empty, false otherwise



# File 'lib/nba/collection.rb', line 18


#firstObject?

Returns the first element in the collection

Examples:

collection.first #=> player1

Returns:

  • (Object, nil)

    the first element or nil if empty



# File 'lib/nba/collection.rb', line 25


#lastObject?

Returns the last element in the collection

Examples:

collection.last #=> player3

Returns:

  • (Object, nil)

    the last element or nil if empty



# File 'lib/nba/collection.rb', line 32


#sizeInteger Also known as: length, count

Returns the number of elements in the collection

Examples:

collection.size #=> 3

Returns:

  • (Integer)

    the number of elements



# File 'lib/nba/collection.rb', line 11