philiprehberger-ring_buffer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 78fe92232bb0a8f2fd0e0f813e3cae0b84354a7d4bb564368cb24f87a1db2301
4
+ data.tar.gz: 59e0d616ef34b8768d0ee4a3a5c9d43e0bd64900102562b492083074be618765
5
+ SHA512:
6
+ metadata.gz: 14a8d5350330173fe79afe892ecd084b3928b80d4d7803e237b7b140436710abb8a4c239671bdb6670319d79921af9f9d299c9266a96803805572a9f1f760b97
7
+ data.tar.gz: b98f3a834bcc2caac032b9afa42d71e4f17b85cf0a61f0480e79e877f734e2f9373d2d8c3b7bfdd424fc43da3095cd95ce691c411bbeb7b88de2f25b6cb5fce4
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ All notable changes to this gem will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-03-22
11
+
12
+ ### Added
13
+
14
+ - Initial release
15
+ - Fixed-capacity circular buffer with overflow
16
+ - Push with automatic oldest-entry eviction
17
+ - Statistics: average, sum, min, max
18
+ - Last-n element retrieval
19
+ - Enumerable support
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 philiprehberger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # philiprehberger-ring_buffer
2
+
3
+ [![Tests](https://github.com/philiprehberger/rb-ring-buffer/actions/workflows/ci.yml/badge.svg)](https://github.com/philiprehberger/rb-ring-buffer/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/philiprehberger-ring_buffer.svg)](https://rubygems.org/gems/philiprehberger-ring_buffer)
5
+ [![License](https://img.shields.io/github/license/philiprehberger/rb-ring-buffer)](LICENSE)
6
+
7
+ Fixed-size circular buffer with overflow, statistics, and enumeration
8
+
9
+ ## Requirements
10
+
11
+ - Ruby >= 3.1
12
+
13
+ ## Installation
14
+
15
+ Add to your Gemfile:
16
+
17
+ ```ruby
18
+ gem 'philiprehberger-ring_buffer'
19
+ ```
20
+
21
+ Or install directly:
22
+
23
+ ```bash
24
+ gem install philiprehberger-ring_buffer
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require 'philiprehberger/ring_buffer'
31
+
32
+ buf = Philiprehberger::RingBuffer.new(3)
33
+ buf.push(1)
34
+ buf.push(2)
35
+ buf.push(3)
36
+ buf.push(4) # overwrites 1
37
+ buf.to_a # => [2, 3, 4]
38
+ buf.full? # => true
39
+ ```
40
+
41
+ ### Statistics
42
+
43
+ ```ruby
44
+ buf = Philiprehberger::RingBuffer.new(100)
45
+ [10, 20, 30].each { |v| buf.push(v) }
46
+
47
+ buf.average # => 20.0
48
+ buf.sum # => 60
49
+ buf.min # => 10
50
+ buf.max # => 30
51
+ ```
52
+
53
+ ### Last N Elements
54
+
55
+ ```ruby
56
+ buf = Philiprehberger::RingBuffer.new(10)
57
+ (1..10).each { |v| buf.push(v) }
58
+ buf.last(3) # => [8, 9, 10]
59
+ ```
60
+
61
+ ### Enumerable
62
+
63
+ ```ruby
64
+ buf = Philiprehberger::RingBuffer.new(5)
65
+ [1, 2, 3].each { |v| buf.push(v) }
66
+ buf.map { |v| v * 2 } # => [2, 4, 6]
67
+ buf.select(&:odd?) # => [1, 3]
68
+ ```
69
+
70
+ ## API
71
+
72
+ | Method | Description |
73
+ |--------|-------------|
74
+ | `RingBuffer.new(capacity)` | Create a buffer with fixed capacity |
75
+ | `#push(value)` | Add a value, overwriting oldest if full |
76
+ | `#to_a` | Convert to array (oldest first) |
77
+ | `#size` | Number of elements in the buffer |
78
+ | `#full?` | Whether the buffer is at capacity |
79
+ | `#empty?` | Whether the buffer has no elements |
80
+ | `#average` | Average of numeric elements |
81
+ | `#sum` | Sum of numeric elements |
82
+ | `#min` | Minimum element |
83
+ | `#max` | Maximum element |
84
+ | `#last(n)` | Last n elements (most recent) |
85
+
86
+ ## Development
87
+
88
+ ```bash
89
+ bundle install
90
+ bundle exec rspec # Run tests
91
+ bundle exec rubocop # Check code style
92
+ ```
93
+
94
+ ## License
95
+
96
+ MIT
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ class RingBuffer
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'ring_buffer/version'
4
+
5
+ module Philiprehberger
6
+ class RingBuffer
7
+ include Enumerable
8
+
9
+ class Error < StandardError; end
10
+
11
+ attr_reader :capacity
12
+
13
+ # Create a new ring buffer with fixed capacity
14
+ #
15
+ # @param capacity [Integer] maximum number of elements
16
+ def initialize(capacity)
17
+ raise Error, 'capacity must be a positive integer' unless capacity.is_a?(Integer) && capacity.positive?
18
+
19
+ @capacity = capacity
20
+ @buffer = Array.new(capacity)
21
+ @head = 0
22
+ @count = 0
23
+ end
24
+
25
+ # Push a value into the buffer, overwriting oldest if full
26
+ #
27
+ # @param value [Object]
28
+ # @return [self]
29
+ def push(value)
30
+ @buffer[@head] = value
31
+ @head = (@head + 1) % @capacity
32
+ @count += 1 if @count < @capacity
33
+ self
34
+ end
35
+
36
+ # Convert buffer contents to an array (oldest first)
37
+ #
38
+ # @return [Array]
39
+ def to_a
40
+ return [] if @count.zero?
41
+
42
+ if @count < @capacity
43
+ @buffer[0...@count]
44
+ else
45
+ start = @head
46
+ Array.new(@capacity) { |i| @buffer[(start + i) % @capacity] }
47
+ end
48
+ end
49
+
50
+ # Number of elements currently in the buffer
51
+ #
52
+ # @return [Integer]
53
+ def size
54
+ @count
55
+ end
56
+
57
+ # Check if the buffer is full
58
+ #
59
+ # @return [Boolean]
60
+ def full?
61
+ @count == @capacity
62
+ end
63
+
64
+ # Check if the buffer is empty
65
+ #
66
+ # @return [Boolean]
67
+ def empty?
68
+ @count.zero?
69
+ end
70
+
71
+ # Calculate average of numeric elements
72
+ #
73
+ # @return [Float]
74
+ def average
75
+ raise Error, 'buffer is empty' if empty?
76
+
77
+ sum.to_f / @count
78
+ end
79
+
80
+ # Calculate sum of numeric elements
81
+ #
82
+ # @return [Numeric]
83
+ def sum
84
+ raise Error, 'buffer is empty' if empty?
85
+
86
+ to_a.sum
87
+ end
88
+
89
+ # Find minimum element
90
+ #
91
+ # @return [Object]
92
+ def min
93
+ raise Error, 'buffer is empty' if empty?
94
+
95
+ to_a.min
96
+ end
97
+
98
+ # Find maximum element
99
+ #
100
+ # @return [Object]
101
+ def max
102
+ raise Error, 'buffer is empty' if empty?
103
+
104
+ to_a.max
105
+ end
106
+
107
+ # Return the last n elements (most recent)
108
+ #
109
+ # @param n [Integer] number of elements
110
+ # @return [Array]
111
+ def last(n = 1)
112
+ arr = to_a
113
+ arr.last(n)
114
+ end
115
+
116
+ # Iterate over elements (oldest first)
117
+ #
118
+ # @yield [element]
119
+ def each(&block)
120
+ to_a.each(&block)
121
+ end
122
+ end
123
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: philiprehberger-ring_buffer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Philip Rehberger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-03-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Fixed-capacity ring buffer that overwrites oldest entries on overflow,
14
+ with built-in statistics (average, sum, min, max), last-n retrieval, and Enumerable
15
+ support.
16
+ email:
17
+ - me@philiprehberger.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - CHANGELOG.md
23
+ - LICENSE
24
+ - README.md
25
+ - lib/philiprehberger/ring_buffer.rb
26
+ - lib/philiprehberger/ring_buffer/version.rb
27
+ homepage: https://github.com/philiprehberger/rb-ring-buffer
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ homepage_uri: https://github.com/philiprehberger/rb-ring-buffer
32
+ source_code_uri: https://github.com/philiprehberger/rb-ring-buffer
33
+ changelog_uri: https://github.com/philiprehberger/rb-ring-buffer/blob/main/CHANGELOG.md
34
+ bug_tracker_uri: https://github.com/philiprehberger/rb-ring-buffer/issues
35
+ rubygems_mfa_required: 'true'
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 3.1.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.5.22
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Fixed-size circular buffer with overflow, statistics, and enumeration
55
+ test_files: []