Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File/IO readlines supporting new params limit, and open mode #650

Merged
merged 2 commits into from Apr 24, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 65 additions & 7 deletions src/org/jruby/RubyIO.java
Expand Up @@ -3321,15 +3321,38 @@ public IRubyObject each_line(final ThreadContext context, IRubyObject[]args, fin
return block.isGiven() ? each_lineInternal(context, args, block) : enumeratorize(context.runtime, this, "each_line", args); return block.isGiven() ? each_lineInternal(context, args, block) : enumeratorize(context.runtime, this, "each_line", args);
} }


@JRubyMethod(optional = 1) @JRubyMethod(name = "readlines", optional = 1, compat = RUBY1_8)
public RubyArray readlines(ThreadContext context, IRubyObject[] args) { public RubyArray readlines(ThreadContext context, IRubyObject[] args) {
return readlinesCommon(context, args);
}

@JRubyMethod(name = "readlines", optional = 2, compat = RUBY1_9)
public RubyArray readlines19(ThreadContext context, IRubyObject[] args) {
return readlinesCommon(context, args);
}

private RubyArray readlinesCommon(ThreadContext context, IRubyObject[] args) {
Ruby runtime = context.runtime; Ruby runtime = context.runtime;
IRubyObject[] separatorArgs = args.length > 0 ? new IRubyObject[] { args[0] } : IRubyObject.NULL_ARRAY;
IRubyObject[] separatorArgs = IRubyObject.NULL_ARRAY;
long limit = -1;

if (args.length > 1) {
separatorArgs = new IRubyObject[] { args[0] };
limit = RubyNumeric.num2long(args[1]);
} else if (args.length > 0) {
if (args[0] instanceof RubyFixnum) {
limit = RubyNumeric.num2long(args[0]);
} else {
separatorArgs = new IRubyObject[] { args[0] };
}
}

ByteList separator = getSeparatorForGets(runtime, separatorArgs); ByteList separator = getSeparatorForGets(runtime, separatorArgs);
RubyArray result = runtime.newArray(); RubyArray result = runtime.newArray();
IRubyObject line; IRubyObject line;


while (! (line = getline(runtime, separator)).isNil()) { while (! (line = getline(runtime, separator, limit, null)).isNil()) {
result.append(line); result.append(line);
} }
return result; return result;
Expand Down Expand Up @@ -3707,16 +3730,51 @@ public static IRubyObject writeStatic(ThreadContext context, IRubyObject recv, I


return write19(context, recv, path, str, offset, (RubyHash) options); return write19(context, recv, path, str, offset, (RubyHash) options);
} }

@JRubyMethod(name = "readlines", required = 1, optional = 1, meta = true) @JRubyMethod(name = "readlines", required = 1, optional = 1, meta = true, compat = RUBY1_8)
public static RubyArray readlines(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) { public static RubyArray readlines(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
int count = args.length; int count = args.length;


IRubyObject[] fileArguments = new IRubyObject[]{ args[0].convertToString() }; IRubyObject[] fileArguments = new IRubyObject[]{ args[0].convertToString() };
IRubyObject[] separatorArguments = count >= 2 ? new IRubyObject[]{args[1]} : IRubyObject.NULL_ARRAY; IRubyObject[] separatorArguments = count >= 2 ? new IRubyObject[]{args[1]} : IRubyObject.NULL_ARRAY;
RubyIO file = (RubyIO) RubyKernel.open(context, recv, fileArguments, Block.NULL_BLOCK);
return readlinesCommon(context, recv, fileArguments, separatorArguments);
}

@JRubyMethod(name = "readlines", required = 1, optional = 3, meta = true, compat = RUBY1_9)
public static RubyArray readlines19(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block unusedBlock) {
int count = args.length;

IRubyObject[] openFileArguments = null;
IRubyObject[] methodArguments = IRubyObject.NULL_ARRAY;

RubyString path = RubyFile.get_path(context, args[0]);

if(count >= 4 && (args[3] instanceof RubyHash || args[3].respondsTo("to_hash")) ) {
openFileArguments = new IRubyObject[]{ path, args[3] };
} else if (count >= 3 && (args[2] instanceof RubyHash || args[2].respondsTo("to_hash") )) {
openFileArguments = new IRubyObject[]{ path, args[2] };
} else if(count >= 2 && (args[1] instanceof RubyHash || args[1].respondsTo("to_hash"))){
openFileArguments = new IRubyObject[]{ path, args[1] };
} else {
openFileArguments = new IRubyObject[]{ path };
}

if(count >= 3 && (args[2] instanceof RubyFixnum || args[2].respondsTo("to_int"))) {
methodArguments = new IRubyObject[]{args[1], args[2]};
} else if (count >= 2 && (args[1] instanceof RubyFixnum || args[1].respondsTo("to_int"))) {
methodArguments = new IRubyObject[]{args[1]};
} else if (count >= 2 && !(args[1] instanceof RubyHash)) {
methodArguments = new IRubyObject[]{args[1]};
}

return readlinesCommon(context, recv, openFileArguments, methodArguments);
}

private static RubyArray readlinesCommon(ThreadContext context, IRubyObject recv, IRubyObject[] openFileArguments , IRubyObject[] methodArguments) {
RubyIO file = (RubyIO) RubyKernel.open(context, recv, openFileArguments, Block.NULL_BLOCK);
try { try {
return file.readlines(context, separatorArguments); return (RubyArray) file.callMethod("readlines", methodArguments);
} finally { } finally {
file.close(); file.close();
} }
Expand Down