Skip to content


add support for options and not truncating offset for IO.write #764

merged 1 commit into from

2 participants

JRuby Team member

No description provided.

JRuby Team member

This change introduces the following rubyspec error:

     [java] IO.write disregards other options if :open_args is given ERROR
     [java] IOError: not opened for writing
     [java] org/jruby/ `write'
     [java] org/jruby/ `write'
     [java] /home/travis/build/jruby/jruby/spec/ruby/core/io/write_spec.rb:61:in `(root)'
     [java] org/jruby/ `instance_eval'
     [java] org/jruby/ `all?'
     [java] org/jruby/ `times'
     [java] org/jruby/ `each'
     [java] /home/travis/build/jruby/jruby/spec/ruby/core/io/write_spec.rb:48:in `(root)'
     [java] /home/travis/build/jruby/jruby/spec/ruby/core/io/write_spec.rb:47:in `(root)'
     [java] org/jruby/ `load'
     [java] org/jruby/ `instance_eval'
     [java] org/jruby/ `each'
JRuby Team member

@atambo how did you simulate this error? Because my change is not supposed to make it happen, this error should be happening before because IO.write doesn't support "open_args" the specs for "open_args" are tagged. I was planing to support "open_args" after merging this pull request because make reviews easier.

@atambo atambo merged commit e812402 into jruby:master

1 check was pending

Details default The Travis CI build is in progress
@josedonizetti josedonizetti deleted the josedonizetti:io_write branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 8 deletions.
  1. +2 −5 spec/tags/1.9/ruby/core/io/write_tags.txt
  2. +19 −3 src/org/jruby/
7 spec/tags/1.9/ruby/core/io/write_tags.txt
@@ -1,6 +1,3 @@
-fails:IO.write doesn't truncate the file and writes the given string if an offset is given
-fails:IO.write accepts a :mode option
-fails:IO.write raises an error if readonly mode is specified
-fails:IO.write uses encoding from given options, if provided
fails:IO.write uses an :open_args option
-fails:IO.write doesn't truncate and writes at the given offset after passing empty opts
+fails:IO.write disregards other options if :open_args is given
22 src/org/jruby/
@@ -3604,12 +3604,28 @@ private static IRubyObject read19(ThreadContext context, IRubyObject recv, IRuby
* open_args: array of string
private static IRubyObject write19(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject str, IRubyObject offset, RubyHash options) {
- // FIXME: process options
RubyString pathStr = RubyFile.get_path(context, path);
Ruby runtime = context.runtime;
failIfDirectory(runtime, pathStr);
- RubyIO file = newFile(context, recv, pathStr, context.runtime.newString("w"));
+ RubyIO file = null;
+ long mode = ModeFlags.CREAT;
+ if (options == null || (options != null && options.isEmpty())) {
+ if (offset.isNil()) {
+ mode |= ModeFlags.WRONLY;
+ } else {
+ mode |= ModeFlags.RDWR;
+ }
+ file = (RubyIO) Helpers.invoke(context, runtime.getFile(), "new", path, RubyFixnum.newFixnum(runtime, mode));
+ } else if (!options.containsKey(runtime.newSymbol("mode"))) {
+ mode |= ModeFlags.WRONLY;
+ file = (RubyIO) Helpers.invoke(context, runtime.getFile(), "new", path, RubyFixnum.newFixnum(runtime, mode), options);
+ } else {
+ file = (RubyIO) Helpers.invoke(context, runtime.getFile(), "new", path, options);
+ }
try {
if (!offset.isNil()), offset);
Something went wrong with that request. Please try again.