aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-08-11 19:42:34 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-08-11 19:42:34 +0900
commit61e594d1508716d9351f3dd355edef6e86e980d9 (patch)
tree2ee314d6b66cbb862abb3e376aa438dc316acb7e /lib
parent1bb4b371509127fdfe3c59a906e2156b51054ae1 (diff)
downloadplum-61e594d1508716d9351f3dd355edef6e86e980d9.tar.gz
binary_string: add String#each_byteslice
Diffstat (limited to 'lib')
-rw-r--r--lib/plum/binary_string.rb15
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