summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-23 06:25:39 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-10-23 06:25:39 +0000
commitaeaeb4b0686b01a4a56998a4b7d87da470a57209 (patch)
tree4100bc0e185938ce00c399a602e8e4f42f0bbdbb
parent5682bc6ac547af13e2d2a4100c7d82995c2749f4 (diff)
stringio.c: write multiple arguments
* ext/stringio/stringio.c (strio_write_m): make StringIO#write accept multiple arguments, as well as IO#write. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60377 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--NEWS3
-rw-r--r--ext/stringio/stringio.c18
-rw-r--r--test/stringio/test_stringio.rb10
3 files changed, 28 insertions, 3 deletions
diff --git a/NEWS b/NEWS
index 09a6f01..db48d26 100644
--- a/NEWS
+++ b/NEWS
@@ -204,6 +204,9 @@ with all sufficient information, see the ChangeLog file or Redmine
* Add Set#=== as alias to #include? [Feature #13801]
* Add Set#reset [Feature #6589]
+* StringIO
+ * StringIO#write accepts multiple arguments
+
* WEBrick
* Add Server Name Indication (SNI) support [Feature #13729]
diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c
index 761ec8e..d6c5545 100644
--- a/ext/stringio/stringio.c
+++ b/ext/stringio/stringio.c
@@ -35,6 +35,7 @@ struct StringIO {
static VALUE strio_init(int, VALUE *, struct StringIO *, VALUE);
static VALUE strio_unget_bytes(struct StringIO *, const char *, long);
+static long strio_write(VALUE self, VALUE str);
#define IS_STRIO(obj) (rb_typeddata_is_kind_of((obj), &strio_data_type))
#define error_inval(msg) (rb_syserr_fail(EINVAL, msg))
@@ -1256,6 +1257,17 @@ strio_readlines(int argc, VALUE *argv, VALUE self)
* Returns the number of bytes written. See IO#write.
*/
static VALUE
+strio_write_m(int argc, VALUE *argv, VALUE self)
+{
+ long len = 0;
+ while (argc-- > 0) {
+ /* StringIO can't exceed long limit */
+ len += strio_write(self, *argv++);
+ }
+ return LONG2NUM(len);
+}
+
+static long
strio_write(VALUE self, VALUE str)
{
struct StringIO *ptr = writable(self);
@@ -1271,7 +1283,7 @@ strio_write(VALUE self, VALUE str)
str = rb_str_conv_enc(str, enc2, enc);
}
len = RSTRING_LEN(str);
- if (len == 0) return INT2FIX(0);
+ if (len == 0) return 0;
check_modifiable(ptr);
olen = RSTRING_LEN(ptr->string);
if (ptr->flags & FMODE_APPEND) {
@@ -1294,7 +1306,7 @@ strio_write(VALUE self, VALUE str)
OBJ_INFECT(ptr->string, self);
RB_GC_GUARD(str);
ptr->pos += len;
- return LONG2NUM(len);
+ return len;
}
/*
@@ -1668,7 +1680,7 @@ Init_stringio(void)
rb_define_method(StringIO, "readlines", strio_readlines, -1);
rb_define_method(StringIO, "read", strio_read, -1);
- rb_define_method(StringIO, "write", strio_write, 1);
+ rb_define_method(StringIO, "write", strio_write_m, -1);
rb_define_method(StringIO, "putc", strio_putc, 1);
/*
diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb
index 7b9b110..f97ac87 100644
--- a/test/stringio/test_stringio.rb
+++ b/test/stringio/test_stringio.rb
@@ -206,6 +206,16 @@ class TestStringIO < Test::Unit::TestCase
}
end
+ def test_write_with_multiple_arguments
+ s = ""
+ f = StringIO.new(s, "w")
+ f.write("foo", "bar")
+ f.close
+ assert_equal("foobar", s)
+ ensure
+ f.close unless f.closed?
+ end
+
def test_set_encoding
bug10285 = '[ruby-core:65240] [Bug #10285]'
f = StringIO.new()