https://github.com/8bit-mate/run_length_encoding.rb
A simple gem that does run-length encoding.
https://github.com/8bit-mate/run_length_encoding.rb
gem library rle rle-compression-algorithm ruby run-length-encoding
Last synced: 4 months ago
JSON representation
A simple gem that does run-length encoding.
- Host: GitHub
- URL: https://github.com/8bit-mate/run_length_encoding.rb
- Owner: 8bit-mate
- License: mit
- Created: 2022-12-14T06:56:30.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-21T14:42:05.000Z (over 1 year ago)
- Last Synced: 2025-02-04T04:31:16.904Z (5 months ago)
- Topics: gem, library, rle, rle-compression-algorithm, ruby, run-length-encoding
- Language: Ruby
- Homepage:
- Size: 55.7 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# run_length_encoding_rb [](https://badge.fury.io/rb/run_length_encoding_rb)
## Description
Run-length encoding for Ruby.
## Installation
Add this line to your application"s Gemfile:
```ruby
gem "run_length_encoding_rb"
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install run_length_encoding_rb
## Usage
### Encoding
RunLengthEncodingRb.encode(data [, separator]) -> array
#### Arguments
+ _data_
Data to encode. Supported types:
- Array;
- String;
- Enumerator.+ _separator_
A String or Regexp to split the _data_ string into single elements. Used only if _data_ is a String.
#### Returns
+ _Array\_
Encoded data. Each element is a RunLengthEncodingRb::RLEElement object with the attributes #chunk (the repeated element) and #run_length (how many times the element is repeated).
### Encoding examples
```ruby
require "run_length_encoding_rb"RLE = RunLengthEncodingRb
# Encode an array:
a = %w[foo foo bar foo foo foo]
RLE.encode(a)# Encode a string with a default separator (each character
# will be treated as a single element):
str = "foo"
RLE.encode(str)# Encode a string with a explicit separator:
str = "foo_foo_bar"
RLE.encode(str, "_")# Encode an enumerator:
str = "foo"
RLE.encode(str.each_byte)
```### Decoding
obj.decode(data) -> array
#### Arguments
+ _data_
Array of RunLengthEncodingRb::RLEElement (or any duck-typed objects, which have the obj#chunk and obj#run_length attributes).
#### Returns
+ Array\
Decoded data.
### Decoding example
```ruby
require "run_length_encoding_rb"RLE = RunLengthEncodingRb
data = [
RunLengthEncodingRb::RLEElement.new(chunk: "foo", run_length: 3),
RunLengthEncodingRb::RLEElement.new(chunk: "bar", run_length: 1),
RunLengthEncodingRb::RLEElement.new(chunk: nil, run_length: 2),
]RLE.decode(data)
# => ["foo", "foo", "foo", "bar", nil, nil]
```## Development
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/8bit-mate/run_length_encoding.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).