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

Commits on Feb 3, 2015

  1. Added spec for Kernel#inspect.

    In MRI, Kernel#inspect checks if no instance variables are defined and
    if that's true, side-calls to the C function implementing Kernel#to_s.
    
    https://github.com/ruby/ruby/blob/e28c3d5df4f5abc83e0d2de34e7ebf675c96a307/object.c#L564
    
    Consequently, when Kernel#to_s is overridden, that Ruby method is never
    called.
    
    In Rubinius, we used the same logic, but actually called the Ruby method
    Kernel#to_s. If the method was overridden, the new method would be called.
    brixen committed Feb 3, 2015
    Copy the full SHA
    bd30650 View commit details
  2. Copy the full SHA
    bbe403e View commit details
Showing with 20 additions and 10 deletions.
  1. +10 −10 kernel/common/kernel.rb
  2. +10 −0 spec/ruby/core/kernel/inspect_spec.rb
20 changes: 10 additions & 10 deletions kernel/common/kernel.rb
Original file line number Diff line number Diff line change
@@ -372,19 +372,19 @@ def initialize_copy(source)
private :initialize_copy

def inspect
# The protocol here seems odd, but it's to match MRI.
prefix = "#<#{self.class}:0x#{self.__id__.to_s(16)}"

# The protocol here seems odd, but it's to match MRI.
#
# MRI side-calls to the C function that implements Kernel#to_s. If that
# method is overridden, the new Ruby method is never called. So, we inline
# the code for Kernel#to_s here because we simply dispatch to Ruby
# methods.
ivars = __instance_variables__

# If this object has no ivars, then we call to_s and return that.
# NOTE MRI has an additional check for when to call to_s. It also does:
# "if (TYPE(obj) == T_OBJECT ..." ie, for all builtin types, don't run
# this code. I'm going to ignore that for now, since the builtin types
# generally have their own inspect anyway, and if for some reason things
# get here, thats ok.
return to_s if ivars.empty?

prefix = "#<#{self.class}:0x#{self.__id__.to_s(16)}"
if ivars.empty?
return Rubinius::Type.infect prefix, self
end

# Otherwise, if it's already been inspected, return the ...
return "#{prefix} ...>" if Thread.guarding? self
10 changes: 10 additions & 0 deletions spec/ruby/core/kernel/inspect_spec.rb
Original file line number Diff line number Diff line change
@@ -13,4 +13,14 @@
it "returns an untrusted string if self is untrusted" do
Object.new.untrust.inspect.untrusted?.should be_true
end

it "does not call #to_s if it is defined" do
# We must use a bare Object here
obj = Object.new
inspected = obj.inspect

obj.stub!(:to_s).and_return("to_s'd")

obj.inspect.should == inspected
end
end