Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 39fcae26f582
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ecf16d2d5d19
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Jan 22, 2018

  1. Copy the full SHA
    da12660 View commit details
  2. Implement IO#write variant that accept multiple arguments

    For more information, please see feature #9323.
    nomadium committed Jan 22, 2018
    Copy the full SHA
    561259b View commit details

Commits on Jan 29, 2018

  1. Merge pull request #4999 from nomadium/io-write-accepts-multiple-argu…

    …ments
    
    IO#write accepts multiple arguments
    enebo authored Jan 29, 2018
    Copy the full SHA
    ecf16d2 View commit details
Showing with 66 additions and 0 deletions.
  1. +9 −0 core/src/main/java/org/jruby/RubyIO.java
  2. +57 −0 test/mri/ruby/test_io.rb
9 changes: 9 additions & 0 deletions core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
@@ -1450,6 +1450,15 @@ public IRubyObject write(ThreadContext context, IRubyObject str) {
return write(context, str, false);
}

@JRubyMethod(name = "write", rest = true)
public IRubyObject write(ThreadContext context, IRubyObject[] args) {
long bytes = Arrays.stream(args)
.map(s -> write(context, s, false))
.map(n -> RubyNumeric.num2long(n))
.reduce(0l, (x, y) -> x + y);
return RubyFixnum.newFixnum(context.runtime, bytes);
}

// io_write
public IRubyObject write(ThreadContext context, IRubyObject str, boolean nosync) {
Ruby runtime = context.runtime;
57 changes: 57 additions & 0 deletions test/mri/ruby/test_io.rb
Original file line number Diff line number Diff line change
@@ -1203,6 +1203,63 @@ def test_ungetc2
end)
end

def test_write_with_multiple_arguments
pipe(proc do |w|
w.write("foo", "bar")
w.close
end, proc do |r|
assert_equal("foobar", r.read)
end)
end

def test_write_with_multiple_arguments_and_buffer
mkcdtmpdir do
line = "x"*9+"\n"
file = "test.out"
open(file, "wb") do |w|
w.write(line)
assert_equal(11, w.write(line, "\n"))
end
open(file, "rb") do |r|
assert_equal([line, line, "\n"], r.readlines)
end

line = "x"*99+"\n"
open(file, "wb") do |w|
w.write(line*81) # 8100 bytes
assert_equal(100, w.write("a"*99, "\n"))
end
open(file, "rb") do |r|
81.times {assert_equal(line, r.gets)}
assert_equal("a"*99+"\n", r.gets)
end
end
end

def test_write_with_many_arguments
[1023, 1024].each do |n|
pipe(proc do |w|
w.write(*(["a"] * n))
w.close
end, proc do |r|
assert_equal("a" * n, r.read)
end)
end
end

def test_write_with_multiple_nonstring_arguments
assert_in_out_err([], "STDOUT.write(:foo, :bar)", ["foobar"])
end

def test_write_buffered_with_multiple_arguments
out, err, (_, status) = EnvUtil.invoke_ruby(["-e", "sleep 0.1;puts 'foo'"], "", true, true) do |_, o, e, i|
[o.read, e.read, Process.waitpid2(i)]
end
assert_predicate(status, :success?)
assert_equal("foo\n", out)
assert_empty(err)
end

def test_write_non_writable
with_pipe do |r, w|
assert_raise(IOError) do