Why Object#inspect is redirecting call to the Kernel module?

Hi,

class String
  remove_method :inspect
end

animal = "cat"
animal.instance_variable_set(:@a, "dog")
p animal.class.ancestors
p animal.method(:inspect)
# >> [String, Comparable, Object, Kernel, BasicObject]
# >> #<Method: String(Kernel)#inspect>

A) Object comes first in the ancestor chains. Why #inspect is then getting called from Kernel while Object#inspect exist ?
B) I didn't find Kernel#inspect here - http://ruby-doc.org/core-2.2.0/Kernel.html . So from where it is coming ?
.
I expected the output as #<Method: String(Object)#inspect>

···

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

irb2.2.0> "".method(:inspect)
#2.2.0 => #<Method: String#inspect>
irb2.2.0> Object.new.method(:inspect)
#2.2.0 => #<Method: Object(Kernel)#inspect>
irb2.2.0> class String; remove_method :inspect; end
#2.2.0 => String
irb2.2.0> "".method(:inspect)
#2.2.0 => #<Method: String(Kernel)#inspect>

Why do you think that #inspect is defined *IN* Object rather than coming from the Kernel module?

-Rob

···

On 2015-Mar-24, at 15:17 , Arup Rakshit <aruprakshit@rocketmail.com> wrote:

Hi,

class String
remove_method :inspect
end

animal = "cat"
animal.instance_variable_set(:@a, "dog")
p animal.class.ancestors
p animal.method(:inspect)
# >> [String, Comparable, Object, Kernel, BasicObject]
# >> #<Method: String(Kernel)#inspect>

A) Object comes first in the ancestor chains. Why #inspect is then getting called from Kernel while Object#inspect exist ?
B) I didn't find Kernel#inspect here - Module: Kernel (Ruby 2.2.0) . So from where it is coming ?
.
I expected the output as #<Method: String(Object)#inspect>

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

I think Arup is refering to the fact that theres an inspect method in
Object as the documentation states.

And there's no inspect method in Kernel documentation.

And if you ask to "toggle source" we see a method definition called
'rb_obj_inspect(VALUE obj)'

IMHO it's confusing.

In Kernel documentation we have a clue:

Abinoam Jr.

···

On Tue, Mar 24, 2015 at 6:04 PM, Rob Biedenharn <rob.biedenharn@gmail.com> wrote:

On 2015-Mar-24, at 15:17 , Arup Rakshit <aruprakshit@rocketmail.com> wrote:

Hi,

class String
remove_method :inspect
end

animal = "cat"
animal.instance_variable_set(:@a, "dog")
p animal.class.ancestors
p animal.method(:inspect)
# >> [String, Comparable, Object, Kernel, BasicObject]
# >> #<Method: String(Kernel)#inspect>

A) Object comes first in the ancestor chains. Why #inspect is then getting called from Kernel while Object#inspect exist ?
B) I didn't find Kernel#inspect here - Module: Kernel (Ruby 2.2.0) . So from where it is coming ?
.
I expected the output as #<Method: String(Object)#inspect>

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

irb2.2.0> "".method(:inspect)
#2.2.0 => #<Method: String#inspect>
irb2.2.0> Object.new.method(:inspect)
#2.2.0 => #<Method: Object(Kernel)#inspect>
irb2.2.0> class String; remove_method :inspect; end
#2.2.0 => String
irb2.2.0> "".method(:inspect)
#2.2.0 => #<Method: String(Kernel)#inspect>

Why do you think that #inspect is defined *IN* Object rather than coming from the Kernel module?

-Rob

[Sorry for stripping the message... repeating...]

I think Arup is refering to the fact that theres an inspect method in
Object as the documentation states.

And there's no inspect method in Kernel documentation.

And if you ask to "toggle source" we see a method definition called
'rb_obj_inspect(VALUE obj)'

IMHO it's confusing.

In Kernel documentation we have a clue:

···

===
Kernel

The Kernel module is included by class Object, so its methods are
available in every Ruby object.
The Kernel instance methods are documented in class Object while the
module methods are documented here.
These methods are called without a receiver and thus can be called in
functional form:

Highlighting
"... The Kernel instance methods are documented in class Object "

Abinoam Jr.

On Tue, Mar 24, 2015 at 11:23 PM, Abinoam Jr. <abinoam@gmail.com> wrote:

I think Arup is refering to the fact that theres an inspect method in
Object as the documentation states.
Class: Object (Ruby 2.2.0)

And there's no inspect method in Kernel documentation.
Module: Kernel (Ruby 2.2.0)

And if you ask to "toggle source" we see a method definition called
'rb_obj_inspect(VALUE obj)'

IMHO it's confusing.

In Kernel documentation we have a clue:

Abinoam Jr.

On Tue, Mar 24, 2015 at 6:04 PM, Rob Biedenharn > <rob.biedenharn@gmail.com> wrote:

On 2015-Mar-24, at 15:17 , Arup Rakshit <aruprakshit@rocketmail.com> wrote:

Hi,

class String
remove_method :inspect
end

animal = "cat"
animal.instance_variable_set(:@a, "dog")
p animal.class.ancestors
p animal.method(:inspect)
# >> [String, Comparable, Object, Kernel, BasicObject]
# >> #<Method: String(Kernel)#inspect>

A) Object comes first in the ancestor chains. Why #inspect is then getting called from Kernel while Object#inspect exist ?
B) I didn't find Kernel#inspect here - Module: Kernel (Ruby 2.2.0) . So from where it is coming ?
.
I expected the output as #<Method: String(Object)#inspect>

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

irb2.2.0> "".method(:inspect)
#2.2.0 => #<Method: String#inspect>
irb2.2.0> Object.new.method(:inspect)
#2.2.0 => #<Method: Object(Kernel)#inspect>
irb2.2.0> class String; remove_method :inspect; end
#2.2.0 => String
irb2.2.0> "".method(:inspect)
#2.2.0 => #<Method: String(Kernel)#inspect>

Why do you think that #inspect is defined *IN* Object rather than coming from the Kernel module?

-Rob

Thanks for showing. I really don't know the actual intention of documenting methods of Kernel inside the Object. As per the Ruby documentation, Object#inspect(Class: Object (Ruby 2.2.1)) is basically Kernel#inspect. Well here something weird I found again.

"The default inspect shows the object’s class name, an encoding of the object id, and a list of the instance variables and their values (by calling inspect on each of them)."

class A
  def initialize
    @x = 12
  end
end

A.new.method(:inspect)
# => #<Method: A(Kernel)#inspect>
A.new.inspect
# => "#<A:0xa92cb88 @x=12>"

Here I am seeing the #inspect as documented. But that's not the case for below example :

class String
  remove_method :inspect
end
s = ""
s.instance_variable_set :@x, 11
# => 11
s.method(:inspect)
# => #<Method: String(Kernel)#inspect>
s.inspect
# => #<String:0xa7e4780>

*Why* `s.inspect` not showing the #<String:0xa7e4780 @x=11> ?

···

On Tuesday, March 24, 2015 11:24:59 PM Abinoam Jr. wrote:

[Sorry for stripping the message... repeating...]

I think Arup is refering to the fact that theres an inspect method in
Object as the documentation states.
Class: Object (Ruby 2.2.0)

And there's no inspect method in Kernel documentation.
Module: Kernel (Ruby 2.2.0)

And if you ask to "toggle source" we see a method definition called
'rb_obj_inspect(VALUE obj)'

IMHO it's confusing.

In Kernel documentation we have a clue:

===
Kernel

The Kernel module is included by class Object, so its methods are
available in every Ruby object.
The Kernel instance methods are documented in class Object while the
module methods are documented here.
These methods are called without a receiver and thus can be called in
functional form:

Highlighting
"... The Kernel instance methods are documented in class Object "

--

Regards,
Arup Rakshit

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan