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: 4fe04fb0c48b
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 34a6af3b527d
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Oct 18, 2016

  1. Copy the full SHA
    a061b5c View commit details
  2. Copy the full SHA
    34a6af3 View commit details
Showing with 49 additions and 8 deletions.
  1. +31 −8 core/src/main/java/org/jruby/RubyRange.java
  2. +6 −0 core/src/main/java/org/jruby/runtime/JavaSites.java
  3. +12 −0 spec/ruby/core/range/shared/include.rb
39 changes: 31 additions & 8 deletions core/src/main/java/org/jruby/RubyRange.java
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@
import org.jruby.runtime.BlockCallback;
import org.jruby.runtime.CallBlock;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.JavaSites;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ObjectMarshal;
import org.jruby.runtime.Signature;
@@ -477,18 +478,16 @@ public IRubyObject each(final ThreadContext context, final Block block) {
return enumeratorizeWithSize(context, this, "each", enumSizeFn(context));
}
final Ruby runtime = context.runtime;
if (begin instanceof RubyTime) {
throw runtime.newTypeError("can't iterate from Time");
} else if (begin instanceof RubyFixnum && end instanceof RubyFixnum) {
if (begin instanceof RubyFixnum && end instanceof RubyFixnum) {
fixnumEach(context, runtime, block);
} else if (begin instanceof RubySymbol) {
} else if (begin instanceof RubySymbol && end instanceof RubySymbol) {
begin.asString().uptoCommon(context, end.asString(), isExclusive, block, true);
} else {
IRubyObject tmp = begin.checkStringType();
if (!tmp.isNil()) {
((RubyString) tmp).uptoCommon(context, end, isExclusive, block);
} else {
if (!begin.respondsTo("succ")) {
if (!discreteObject(context, begin)) {
throw runtime.newTypeError("can't iterate from "
+ begin.getMetaClass().getName());
}
@@ -682,11 +681,16 @@ public IRubyObject include_p19(ThreadContext context, IRubyObject obj) {
@JRubyMethod(name = {"include?", "member?"}, frame = true)
public IRubyObject include_p(ThreadContext context, final IRubyObject obj) {
final Ruby runtime = context.runtime;
if ( begin instanceof RubyNumeric || end instanceof RubyNumeric
|| ! TypeConverter.convertToTypeWithCheck(begin, runtime.getInteger(), "to_int").isNil()
|| ! TypeConverter.convertToTypeWithCheck(end, runtime.getInteger(), "to_int").isNil() ) {

boolean iterable = begin instanceof RubyNumeric || end instanceof RubyNumeric ||
linearObject(context, begin) || linearObject(context, end);

if (iterable
|| !TypeConverter.convertToTypeWithCheck(context, begin, runtime.getInteger(), sites(context).to_int_checked).isNil()
|| !TypeConverter.convertToTypeWithCheck(context, end, runtime.getInteger(), sites(context).to_int_checked).isNil()) {
return cover_p(context, obj);
}

if ( begin instanceof RubyString && end instanceof RubyString
&& ((RubyString) begin).getByteList().getRealSize() == 1
&& ((RubyString) end).getByteList().getRealSize() == 1 ) {
@@ -707,9 +711,24 @@ public IRubyObject include_p(ThreadContext context, final IRubyObject obj) {
}
}
}

return Helpers.invokeSuper(context, this, obj, Block.NULL_BLOCK);
}

private static boolean discreteObject(ThreadContext context, IRubyObject obj) {
if (obj instanceof RubyTime) return false;
return sites(context).respond_to_succ.respondsTo(context, obj, obj, false);
}

private static boolean linearObject(ThreadContext context, IRubyObject obj) {
if (obj instanceof RubyFixnum || obj instanceof RubyFloat) return true;
// if (SPECIAL_CONST_P(obj)) return FALSE;
if (obj instanceof RubyBignum) return true;
if (obj instanceof RubyNumeric) return true;
if (obj instanceof RubyTime) return true;
return false;
}

@JRubyMethod(name = "===")
public IRubyObject eqq_p(ThreadContext context, IRubyObject obj) {
return callMethod(context, "include?", obj);
@@ -912,4 +931,8 @@ public static boolean isRangeLike(ThreadContext context, IRubyObject obj, Respon
return respond_to_begin.respondsTo(context, obj, obj) &&
respond_to_end.respondsTo(context, obj, obj);
}

private static JavaSites.RangeSites sites(ThreadContext context) {
return context.sites.Range;
}
}
6 changes: 6 additions & 0 deletions core/src/main/java/org/jruby/runtime/JavaSites.java
Original file line number Diff line number Diff line change
@@ -36,6 +36,7 @@ public class JavaSites {
public final BigDecimalSites BigDecimal = new BigDecimalSites();
public final ComplexSites Complex = new ComplexSites();
public final RationalSites Rational = new RationalSites();
public final RangeSites Range = new RangeSites();
public final ZlibSites Zlib = new ZlibSites();

public static class BasicObjectSites {
@@ -364,6 +365,11 @@ public static class RationalSites {
public final CheckedSites to_r_checked = new CheckedSites("to_r");
}

public static class RangeSites {
public final RespondToCallSite respond_to_succ = new RespondToCallSite("succ");
public final CheckedSites to_int_checked = new CheckedSites("to_int");
}

public static class ZlibSites {
public final RespondToCallSite reader_respond_to = new RespondToCallSite();
public final RespondToCallSite writer_respond_to = new RespondToCallSite();
12 changes: 12 additions & 0 deletions spec/ruby/core/range/shared/include.rb
Original file line number Diff line number Diff line change
@@ -76,4 +76,16 @@
end
end
end

describe "with Time endpoints" do
it "uses cover? logic" do
now = Time.now
range = (now..(now + 60))

range.include?(now).should == true
range.include?(now - 1).should == false
range.include?(now + 60).should == true
range.include?(now + 61).should == false
end
end
end