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: 425743f0d865
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4e6b3300cc96
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Jan 5, 2018

  1. Copy the full SHA
    6858258 View commit details
  2. Merge pull request #4936 from ChrisBr/ruby-2.5

    Implement Dir.glob base option
    
    @ChrisBr If you look into canon. needs and need to change it a new PR can take care of that.
    enebo authored Jan 5, 2018
    Copy the full SHA
    4e6b330 View commit details
Showing with 36 additions and 5 deletions.
  1. +26 −5 core/src/main/java/org/jruby/RubyDir.java
  2. +10 −0 spec/ruby/core/dir/glob_spec.rb
31 changes: 26 additions & 5 deletions core/src/main/java/org/jruby/RubyDir.java
Original file line number Diff line number Diff line change
@@ -62,6 +62,7 @@
import org.jruby.util.JRubyFile;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;
import org.jruby.util.TypeConverter;

/**
* .The Ruby built-in class Dir.
@@ -206,17 +207,37 @@ private static ByteList globArgumentAsByteList(ThreadContext context, IRubyObjec
* with each filename is passed to the block in turn. In this case, Nil is
* returned.
*/
@JRubyMethod(required = 1, optional = 1, meta = true)
@JRubyMethod(required = 1, optional = 2, meta = true)
public static IRubyObject glob(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
Ruby runtime = context.runtime;
int flags = args.length == 2 ? RubyNumeric.num2int(args[1]) : 0;
int flags = 0;
String dir, base = "";

if(args.length == 3) {
RubyHash hash = (RubyHash) args[2];
base = (String) hash.get(context.runtime.newSymbol("base"));
flags = RubyNumeric.num2int(args[1]);
} else if(args.length == 2) {
IRubyObject tmp = TypeConverter.checkHashType(runtime, args[1]);
if(tmp.isNil()){
flags = RubyNumeric.num2int(args[1]);
} else {
RubyHash hash = (RubyHash) args[1];
base = (String) hash.get(context.runtime.newSymbol("base"));
}
}

List<ByteList> dirs;
IRubyObject tmp = args[0].checkArrayType();
if (tmp.isNil()) {
dirs = Dir.push_glob(runtime, runtime.getCurrentDirectory(), globArgumentAsByteList(context, args[0]), flags);

if (!base.isEmpty() && !(new File(base).exists())){
dirs = new ArrayList<ByteList>();
} else if (tmp.isNil()) {
dir = base.isEmpty() ? runtime.getCurrentDirectory() : base;
dirs = Dir.push_glob(runtime, dir, globArgumentAsByteList(context, args[0]), flags);
} else {
dirs = dirGlobs(context, getCWD(runtime), ((RubyArray) tmp).toJavaArray(), flags);
dir = base.isEmpty() ? getCWD(runtime) : base;
dirs = dirGlobs(context, dir, ((RubyArray) tmp).toJavaArray(), flags);
}

if (block.isGiven()) {
10 changes: 10 additions & 0 deletions spec/ruby/core/dir/glob_spec.rb
Original file line number Diff line number Diff line change
@@ -153,4 +153,14 @@
end
end
end

ruby_version_is "2.5" do
it "accepts base: parameter" do
Dir.mkdir 'foo'
dir = File.join(@cwd, foo)
files = %w[a/foo.c c/bar.c].map {|n| File.join(dir, n)}
files.each {|n| File.write(n, "")}
assert_equal(files, Dir.glob("*/*.c", base: dir))
end
end
end