diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-08-11 19:42:34 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-08-11 19:42:34 +0900 |
commit | 61e594d1508716d9351f3dd355edef6e86e980d9 (patch) | |
tree | 2ee314d6b66cbb862abb3e376aa438dc316acb7e /lib | |
parent | 1bb4b371509127fdfe3c59a906e2156b51054ae1 (diff) | |
download | plum-61e594d1508716d9351f3dd355edef6e86e980d9.tar.gz |
binary_string: add String#each_byteslice
Diffstat (limited to 'lib')
-rw-r--r-- | lib/plum/binary_string.rb | 15 |
1 files changed, 15 insertions, 0 deletions
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 |