Skip to content

Commit

Permalink
Minor tweaks to re-arity split some changes from #5915.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Oct 27, 2019
1 parent 8a75d31 commit 3c1dbc3
Showing 1 changed file with 65 additions and 26 deletions.
91 changes: 65 additions & 26 deletions core/src/main/java/org/jruby/RubyDir.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,37 +126,64 @@ private final void checkDirIgnoreClosed() {
* of the directory will not be reflected during the lifetime of the
* <code>Dir</code> object returned, so a new <code>Dir</code> instance
* must be created to reflect changes to the underlying file system.
*
* @param context current context
* @param path target path
* @return a new Dir object
*/
@JRubyMethod(name = "initialize", required = 1, optional = 1)
public IRubyObject initialize(ThreadContext context, IRubyObject[] args) {
@JRubyMethod(name = "initialize")
public IRubyObject initialize(ThreadContext context, IRubyObject path) {
Ruby runtime = context.runtime;

if (args.length > 1 && !args[1].isNil()) {
RubyHash opts = args[1].convertToHash();
IRubyObject encodingArg = ArgsUtil.extractKeywordArg(context, (RubyHash) opts, "encoding");
return initializeCommon(context, path, runtime.getDefaultFilesystemEncoding(), runtime);
}

/**
* Like {@link #initialize(ThreadContext, IRubyObject)} but accepts an :encoding option.
*
* @param context current context
* @param path target path
* @param encOpts encoding options
* @return a new Dir object
*/
@JRubyMethod(name = "initialize")
public IRubyObject initialize(ThreadContext context, IRubyObject path, IRubyObject encOpts) {
Ruby runtime = context.runtime;

Encoding encoding = null;

if (!encOpts.isNil()) {
RubyHash opts = encOpts.convertToHash();
IRubyObject encodingArg = ArgsUtil.extractKeywordArg(context, opts, "encoding");
if (encodingArg != null && !encodingArg.isNil()) {
encoding = runtime.getEncodingService().getEncodingFromObject(encodingArg);
}
}

if (encoding == null) encoding = runtime.getDefaultFilesystemEncoding();

RubyString newPath = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, args[0]));
path = newPath;
pos = 0;
return initializeCommon(context, path, encoding, runtime);
}

private RubyDir initializeCommon(ThreadContext context, IRubyObject pathArg, Encoding encoding, Ruby runtime) {
RubyString newPath = StringSupport.checkEmbeddedNulls(runtime, RubyFile.get_path(context, pathArg));
this.path = newPath;
this.pos = 0;

this.encoding = encoding;

String adjustedPath = RubyFile.adjustRootPathOnWindows(runtime, newPath.toString(), null);
checkDirIsTwoSlashesOnWindows(getRuntime(), adjustedPath);

dir = JRubyFile.createResource(context, adjustedPath);
snapshot = getEntries(context, dir, adjustedPath);
this.dir = JRubyFile.createResource(context, adjustedPath);
this.snapshot = getEntries(context, dir, adjustedPath);

return this;
}

@Deprecated
public IRubyObject initialize19(ThreadContext context, IRubyObject arg) {
return initialize(context, new IRubyObject[] {arg});
return initialize(context, arg);
}

// ----- Ruby Class Methods ----------------------------------------------------
Expand Down Expand Up @@ -304,7 +331,7 @@ public static IRubyObject glob(ThreadContext context, IRubyObject recv, IRubyObj
* @return all entries for this Dir
*/
@JRubyMethod(name = "entries")
public RubyArray entries() {
public RubyArray entries() {
JavaUtil.StringConverter converter = new JavaUtil.StringConverter(encoding);
return RubyArray.newArrayMayCopy(getRuntime(), JavaUtil.convertStringArrayToRuby(getRuntime(), snapshot, converter));
}
Expand Down Expand Up @@ -403,7 +430,7 @@ public static IRubyObject chdir(ThreadContext context, IRubyObject recv, IRubyOb

String adjustedPath = RubyFile.adjustRootPathOnWindows(runtime, path.asJavaString(), null);
checkDirIsTwoSlashesOnWindows(runtime, adjustedPath);

adjustedPath = getExistingDir(runtime, adjustedPath).canonicalPath();

IRubyObject result;
Expand Down Expand Up @@ -517,14 +544,13 @@ public static IRubyObject foreach(ThreadContext context, IRubyObject recv, IRuby
return foreachCommon(context, recv, RubyFile.get_path(context, path), null, block);
}

@JRubyMethod(name = "foreach", meta = true, required=1, optional=1)
public static IRubyObject foreach(ThreadContext context, IRubyObject recv, IRubyObject [] args, Block block) {
@JRubyMethod(name = "foreach", meta = true)
public static IRubyObject foreach(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject encOpts, Block block) {
Encoding encoding = null;
IRubyObject path = args[0];

if(args.length > 1 && !args[1].isNil()) {
IRubyObject hash = TypeConverter.checkHashType(context.runtime, args[1]);
IRubyObject encodingArg = ArgsUtil.extractKeywordArg(context, (RubyHash) hash, "encoding");

if(!encOpts.isNil()) {
IRubyObject opts = TypeConverter.checkHashType(context.runtime, encOpts);
IRubyObject encodingArg = ArgsUtil.extractKeywordArg(context, (RubyHash) opts, "encoding");
if (encodingArg != null && !encodingArg.isNil()) {
encoding = context.runtime.getEncodingService().getEncodingFromObject(encodingArg);
}
Expand All @@ -537,12 +563,12 @@ public static IRubyObject foreach(ThreadContext context, IRubyObject recv, IRuby

@Deprecated
public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) {
return foreach(context, recv, path, block);
return foreachCommon(context, recv, RubyFile.get_path(context, path), null, block);
}

@Deprecated
public static IRubyObject foreach19(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject enc, Block block) {
return foreach(context, recv, new IRubyObject[] {path, enc}, block);
return foreachCommon(context, recv, RubyFile.get_path(context, path), context.runtime.getEncodingService().getEncodingFromObject(enc), block);
}

private static IRubyObject eachChildCommon(ThreadContext context, IRubyObject recv, RubyString path, RubyEncoding encoding, Block block) {
Expand Down Expand Up @@ -657,9 +683,22 @@ private static IRubyObject mkdirCommon(Ruby runtime, String path, IRubyObject[]
* provided, a new directory object is passed to the block, which closes the
* directory object before terminating.
*/
@JRubyMethod(name = "open", meta = true, required = 1, optional = 1)
public static IRubyObject open(ThreadContext context, IRubyObject recv, IRubyObject args[], Block block) {
RubyDir directory = (RubyDir) context.runtime.getDir().newInstance(context, args, Block.NULL_BLOCK);
@JRubyMethod(name = "open", meta = true)
public static IRubyObject open(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) {
RubyDir directory = (RubyDir) context.runtime.getDir().newInstance(context, path, Block.NULL_BLOCK);

if (!block.isGiven()) return directory;

try {
return block.yield(context, directory);
} finally {
directory.close();
}
}

@JRubyMethod(name = "open", meta = true)
public static IRubyObject open(ThreadContext context, IRubyObject recv, IRubyObject path, IRubyObject encOpts, Block block) {
RubyDir directory = (RubyDir) context.runtime.getDir().newInstance(context, path, encOpts, Block.NULL_BLOCK);

if (!block.isGiven()) return directory;

Expand All @@ -672,7 +711,7 @@ public static IRubyObject open(ThreadContext context, IRubyObject recv, IRubyObj

@Deprecated
public static IRubyObject open19(ThreadContext context, IRubyObject recv, IRubyObject path, Block block) {
return open(context, recv, new IRubyObject[] {path}, block);
return open(context, recv, path, block);
}

// ----- Ruby Instance Methods -------------------------------------------------
Expand Down

0 comments on commit 3c1dbc3

Please sign in to comment.