Skip to content

Commit 3187761

Browse files
committed
RUBY-258 Bytebuffer#unpack takes arbitrary format string
1 parent a19d5bc commit 3187761

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/bson/byte_buffer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ def ==(other)
236236
other.respond_to?(:to_s) && @str == other.to_s
237237
end
238238

239-
def to_a
240-
@str.unpack("C*")
239+
def to_a(format="C*")
240+
@str.unpack(format)
241241
end
242242

243-
def unpack(args)
244-
to_a
243+
def unpack(format="C*")
244+
to_a(format)
245245
end
246246

247247
def to_s

test/bson/byte_buffer_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ def test_nil_get_returns_one_byte
2121
assert_equal 1, @buf.get
2222
end
2323

24+
def test_unpack
25+
@buf.put_array([17, 2, 3, 4])
26+
assert_equal [17, 2, 3, 4], @buf.to_a
27+
assert_equal ["11020304"], @buf.unpack("H*")
28+
assert_equal ["11020304"], @buf.to_a("H*")
29+
end
30+
2431
def test_one_get_returns_array_length_one
2532
@buf.put_array([1, 2, 3, 4])
2633
@buf.rewind

0 commit comments

Comments
 (0)