Class: Splashy::Bucket
- Inherits:
-
Object
- Object
- Splashy::Bucket
- Defined in:
- lib/splashy/bucket.rb
Overview
Private: Collects elements and maintains a count.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #<<(element) ⇒ Object
- #count ⇒ Object
- #elements(count = nil) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(name) ⇒ Bucket
constructor
A new instance of Bucket.
- #random_elements(count = nil) ⇒ Object
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
#name ⇒ Object (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 |
#count ⇒ Object
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
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 |