From cfa154fa577d4e4d0f551b1b3ed9502d24963ec0 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Mon, 23 Oct 2017 16:47:21 +0900 Subject: [PATCH] buffering: let #write accept multiple arguments As of Ruby 2.5, IO#write accepts multiple input strings and writes them at once[1]. Follow that. [1] https://bugs.ruby-lang.org/issues/9323 --- lib/openssl/buffering.rb | 8 +++++--- test/test_pair.rb | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/openssl/buffering.rb b/lib/openssl/buffering.rb index 8b5dd9da5..935f61f0e 100644 --- a/lib/openssl/buffering.rb +++ b/lib/openssl/buffering.rb @@ -339,9 +339,11 @@ def do_write(s) # Writes _s_ to the stream. If the argument is not a String it will be # converted using +.to_s+ method. Returns the number of bytes written. - def write(s) - do_write(s) - s.bytesize + def write(*s) + s.inject(0) do |written, str| + do_write(str) + written + str.bytesize + end end ## diff --git a/test/test_pair.rb b/test/test_pair.rb index 55b62321b..29d5c9bbb 100644 --- a/test/test_pair.rb +++ b/test/test_pair.rb @@ -362,6 +362,15 @@ def test_write_zero } end + def test_write_multiple_arguments + ssl_pair {|s1, s2| + str1 = "foo"; str2 = "bar" + assert_equal 6, s1.write(str1, str2) + s1.close + assert_equal "foobar", s2.read + } + end + def test_partial_tls_record_read_nonblock ssl_pair { |s1, s2| # the beginning of a TLS record