Skip to content

Instantly share code, notes, and snippets.

@OrelSokolov
Created August 27, 2019 09:57
Show Gist options
  • Select an option

  • Save OrelSokolov/94e2ff34e2c9cd8c6c0bb42835dba8d8 to your computer and use it in GitHub Desktop.

Select an option

Save OrelSokolov/94e2ff34e2c9cd8c6c0bb42835dba8d8 to your computer and use it in GitHub Desktop.
Abstract buffer
class Buffer
attr_reader :buffer, :overflows
def initialize(&block)
@limitation_criteria = block
@buffer = []
@overflows = 0
end
def set_action(&block)
@action = block
end
def clean_with_action!
@action.call @buffer
@buffer = []
end
def push(data)
@buffer.push data
if @limitation_criteria.call(@buffer)
@overflows += 1
clean_with_action!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment