Class: Splashy::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/splashy/bucket.rb

Overview

Private: Collects elements and maintains a count.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Bucket

Returns a new instance of Bucket.



8
9
10
11
# File 'lib/splashy/bucket.rb', line 8

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/splashy/bucket.rb', line 6

def name
  @name
end

Instance Method Details

#<<(element) ⇒ Object



13
14
15
# File 'lib/splashy/bucket.rb', line 13

def <<( element )
  @elements << element
end

#countObject



37
38
39
# File 'lib/splashy/bucket.rb', line 37

def count
  @elements.count
end

#elements(count = nil) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/splashy/bucket.rb', line 17

def elements( count = nil )
  if count
    @elements[0, count]
  else
    @elements
  end
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/splashy/bucket.rb', line 33

def empty?
  self.count == 0
end

#random_elements(count = nil) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/splashy/bucket.rb', line 25

def random_elements( count = nil )
  if @elements.respond_to?( :sample )
    @elements.sample( count )
  else
    count ? @elements.sort_by{ rand }[0, count] : @elements.sort_by{ rand }
  end
end