diff --git a/lib/rubella/storage.rb b/lib/rubella/storage.rb index 6aa166b..fc63aa4 100644 --- a/lib/rubella/storage.rb +++ b/lib/rubella/storage.rb @@ -10,23 +10,37 @@ module Rubella attr_reader :data attr_reader :length - def initialize data, length = nil - @data = data + def initialize data, length = 0 + @data = data self.length = length end # Defines the length of the Storage. + # # Be careful, if your Storage has more entries, tha the new length, the - # oldest entries will immediately be dropped. + # oldest entries will immediately be dropped. If your Storage has less + # entries, it will be filled up with empty entries. + # Happens only if data has entries. + # + # Setting the length to 0 will disable this feature + # + # @param length Integer The size of the storage + # @return Integer The new size # def length= length @length = length - # Drop entries, if more than new length - if @length.kind_of? Integer + # Use length only, if length is valid + if @length != 0 and @data.length != 0 + # Drop entries, if more than new length while @data.length > @length @data.pop end + # Prefill with empty content, if less than new length + dummy_data = Array.new(@data[0].length, 0) + while @data.length < @length + @data.unshift dummy_data + end end @length