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])


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



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



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


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


#firstObject?

Returns the first element in the collection

Examples:

collection.first #=> player1


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


#lastObject?

Returns the last element in the collection

Examples:

collection.last #=> player3


# 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


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