Prefill Storage with zero values
Only if Storage already has some data. Disable empty and prefill feature if given lenght is 0.
This commit is contained in:
parent
01c1f97750
commit
2bb29d9e8d
|
@ -10,23 +10,37 @@ module Rubella
|
||||||
attr_reader :data
|
attr_reader :data
|
||||||
attr_reader :length
|
attr_reader :length
|
||||||
|
|
||||||
def initialize data, length = nil
|
def initialize data, length = 0
|
||||||
@data = data
|
@data = data
|
||||||
self.length = length
|
self.length = length
|
||||||
end
|
end
|
||||||
|
|
||||||
# Defines the length of the Storage.
|
# Defines the length of the Storage.
|
||||||
|
#
|
||||||
# Be careful, if your Storage has more entries, tha the new length, the
|
# 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
|
def length= length
|
||||||
@length = length
|
@length = length
|
||||||
|
|
||||||
# Drop entries, if more than new length
|
# Use length only, if length is valid
|
||||||
if @length.kind_of? Integer
|
if @length != 0 and @data.length != 0
|
||||||
|
# Drop entries, if more than new length
|
||||||
while @data.length > @length
|
while @data.length > @length
|
||||||
@data.pop
|
@data.pop
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
@length
|
@length
|
||||||
|
|
Loading…
Reference in New Issue