Skip to content

Commit

Permalink
StringIO#write takes multiple arguments. #4876
Browse files Browse the repository at this point in the history
headius committed Mar 27, 2018

Verified

This commit was signed with the committer’s verified signature.
headius Charles Oliver Nutter
1 parent ab440ad commit be3bd17
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions core/src/main/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
@@ -1163,11 +1163,25 @@ public IRubyObject ungetbyte(ThreadContext context, IRubyObject arg) {
}

// MRI: strio_write
@JRubyMethod(name = {"write"}, required = 1)
@JRubyMethod(name = "write")
public IRubyObject write(ThreadContext context, IRubyObject arg) {
checkWritable();
Ruby runtime = context.runtime;
return RubyFixnum.newFixnum(runtime, stringIOWrite(context, runtime, arg));
}

final Ruby runtime = context.runtime;
@JRubyMethod(name = "write", required = 1, rest = true)
public IRubyObject write(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.runtime;
long len = 0;
for (IRubyObject arg : args) {
len += stringIOWrite(context, runtime, arg);
}
return RubyFixnum.newFixnum(runtime, len);
}

// MRI: strio_write
private long stringIOWrite(ThreadContext context, Ruby runtime, IRubyObject arg) {
checkWritable();

RubyString str = arg.asString();
int len, olen;
@@ -1184,7 +1198,7 @@ public IRubyObject write(ThreadContext context, IRubyObject arg) {
str = EncodingUtils.strConvEnc(context, str, encStr, enc);
}
len = str.size();
if (len == 0) return RubyFixnum.zero(runtime);
if (len == 0) return 0;
checkModifiable();
olen = ptr.string.size();
if ((ptr.flags & OpenFile.APPEND) != 0) {
@@ -1202,7 +1216,7 @@ public IRubyObject write(ThreadContext context, IRubyObject arg) {
ptr.pos += len;
}

return RubyFixnum.newFixnum(runtime, len);
return len;
}

@JRubyMethod

0 comments on commit be3bd17

Please sign in to comment.