class RotationHash

A Hash descendant with elements count limit and rotation

@example Create a new RotationHash with the limit

rh = RotationHash.new(3, 'default_value')
rh['one']      = 1
rh['two']      = 2
rh['three']    = 3
rh['whatever'] = '¯\_(ツ)_/¯'

p rh['inexistent'] # => "default_value"
p rh               # => {"two"=>2, "three"=>3, "whatever"=>"¯\_(ツ)_/¯"}

Constants

VERSION

Current library version

Public Class Methods

new(rotation_limit, *args) click to toggle source

Create a new RotationHash @param [Integer] rotation_limit the max possible elements count @param [Array<Object>] args Any number of Objects to pass to the Hash

@note The first argument used to set a limit all the others passed

to the Hash as is.
Calls superclass method
# File lib/rotation_hash.rb, line 25
def initialize(rotation_limit, *args)
  @rotation_limit = rotation_limit
  super(*args)
end

Public Instance Methods

[]=(key, value) click to toggle source

Overloaded Hash assignment method. Adds elements and on reaching the limit automatically rotates the Hash

@param [Object] key the key to assign the value to @param [Object] value the value to set on the key

@return [Object] the object that was assigned to the key

Calls superclass method
# File lib/rotation_hash.rb, line 37
def []=(key, value)
  shift if length + 1 > @rotation_limit
  super(key, value)
end