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

Commits on Feb 19, 2017

  1. check for Object before triggerring respond_to?(:to_r) check

    ... also added a caching respond_to? call-site (see #4484)
    kares committed Feb 19, 2017
    Copy the full SHA
    001cde2 View commit details
  2. Copy the full SHA
    e9f5f59 View commit details
  3. Copy the full SHA
    7222d94 View commit details
  4. Copy the full SHA
    339c72d View commit details
4 changes: 3 additions & 1 deletion core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
@@ -4258,7 +4258,9 @@ private static long transfer(ThreadContext context, ReadableByteChannel from, Wr

@JRubyMethod(name = "try_convert", meta = true)
public static IRubyObject tryConvert(ThreadContext context, IRubyObject recv, IRubyObject arg) {
return sites(context).respond_to_to_io.respondsTo(context, arg, arg, true) ? convertToIO(context, arg) : context.runtime.getNil();
return ( arg instanceof RubyObject &&
sites(context).respond_to_to_io.respondsTo(context, arg, arg, true) ) ?
convertToIO(context, arg) : context.nil;
}

private static ByteList getNilByteList(Ruby runtime) {
19 changes: 13 additions & 6 deletions core/src/main/java/org/jruby/RubyRational.java
Original file line number Diff line number Diff line change
@@ -76,6 +76,7 @@
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callsite.RespondToCallSite;
import org.jruby.runtime.marshal.MarshalStream;
import org.jruby.runtime.marshal.UnmarshalStream;
import org.jruby.util.ByteList;
@@ -352,21 +353,20 @@ public static IRubyObject convert(ThreadContext context, IRubyObject recv, IRuby

private static IRubyObject convertCommon(ThreadContext context, IRubyObject recv, IRubyObject a1, IRubyObject a2) {
if (a1 instanceof RubyComplex) {
RubyComplex a1Complex = (RubyComplex)a1;
if (k_exact_p(a1Complex.getImage()) && f_zero_p(context, a1Complex.getImage())) a1 = a1Complex.getReal();
RubyComplex a1c = (RubyComplex) a1;
if (k_exact_p(a1c.getImage()) && f_zero_p(context, a1c.getImage())) a1 = a1c.getReal();
}

if (a2 instanceof RubyComplex) {
RubyComplex a2Complex = (RubyComplex)a2;
if (k_exact_p(a2Complex.getImage()) && f_zero_p(context, a2Complex.getImage())) a2 = a2Complex.getReal();
RubyComplex a2c = (RubyComplex) a2;
if (k_exact_p(a2c.getImage()) && f_zero_p(context, a2c.getImage())) a2 = a2c.getReal();
}

if (a1 instanceof RubyFloat) {
a1 = f_to_r(context, a1);
} else if (a1 instanceof RubyString) {
a1 = str_to_r_strict(context, a1);
} else {
if (a1.respondsTo("to_r")) {
if (a1 instanceof RubyObject && responds_to_to_r(context, a1)) {
a1 = f_to_r(context, a1);
}
}
@@ -393,6 +393,13 @@ private static IRubyObject convertCommon(ThreadContext context, IRubyObject recv
}
}

private static boolean responds_to_to_r(ThreadContext context, IRubyObject obj) {
return respond_to_to_r.respondsTo(context, obj, obj);
}

// TODO: wasn't sure whether to put this on NumericSites, here for now - should move
static final RespondToCallSite respond_to_to_r = new RespondToCallSite("to_r");

/** nurat_numerator
*
*/
4 changes: 4 additions & 0 deletions spec/ruby/core/io/try_convert_spec.rb
Original file line number Diff line number Diff line change
@@ -31,6 +31,10 @@
IO.try_convert(mock("io")).should be_nil
end

it "return nil when BasicObject is passed" do
IO.try_convert(BasicObject.new).should be_nil
end

it "raises a TypeError if the object does not return an IO from #to_io" do
obj = mock("io")
obj.should_receive(:to_io).and_return("io")
5 changes: 5 additions & 0 deletions spec/ruby/core/rational/to_r_spec.rb
Original file line number Diff line number Diff line change
@@ -2,4 +2,9 @@

describe "Rational#to_r" do
it_behaves_like(:rational_to_r, :to_r)

it "raises TypeError trying to convert BasicObject" do
obj = BasicObject.new
lambda { Rational(obj) }.should raise_error(TypeError)
end
end