Class: LimitedArray
- Inherits:
-
Array
- Object
- Array
- LimitedArray
- Defined in:
- lib/utils/limited_array.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
Returns the value of attribute limit.
Instance Method Summary collapse
- #<<(val) ⇒ Object
- #average ⇒ Object
- #full? ⇒ Boolean
-
#initialize(limit) ⇒ LimitedArray
constructor
A new instance of LimitedArray.
- #sum ⇒ Object
Constructor Details
#initialize(limit) ⇒ LimitedArray
Returns a new instance of LimitedArray.
5 6 7 8 |
# File 'lib/utils/limited_array.rb', line 5 def initialize(limit) @limit = limit super() end |
Instance Attribute Details
#limit ⇒ Object
Returns the value of attribute limit.
3 4 5 |
# File 'lib/utils/limited_array.rb', line 3 def limit @limit end |
Instance Method Details
#<<(val) ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/utils/limited_array.rb', line 14 def <<(val) if full? self.rotate! self[limit-1] = val else super end end |
#average ⇒ Object
23 24 25 |
# File 'lib/utils/limited_array.rb', line 23 def average sum.to_f / size end |
#full? ⇒ Boolean
10 11 12 |
# File 'lib/utils/limited_array.rb', line 10 def full? size == limit end |
#sum ⇒ Object
27 28 29 |
# File 'lib/utils/limited_array.rb', line 27 def sum inject(:+) end |