From 61e594d1508716d9351f3dd355edef6e86e980d9 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Tue, 11 Aug 2015 19:42:34 +0900 Subject: binary_string: add String#each_byteslice --- lib/plum/binary_string.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib') diff --git a/lib/plum/binary_string.rb b/lib/plum/binary_string.rb index 2d3ed76..72b38b1 100644 --- a/lib/plum/binary_string.rb +++ b/lib/plum/binary_string.rb @@ -54,6 +54,21 @@ module Plum force_encoding(Encoding::BINARY) slice!(0, count) end + + def each_byteslice(n, &blk) + if block_given? + pos = 0 + while pos < self.bytesize + yield byteslice(pos, n) + pos += n + end + else + Enumerator.new do |y| + each_byteslice(n) {|ss| y << ss } + end + # I want to write `enum_for(__method__, n)`! + end + end end end end -- cgit v1.2.3