Unexpected results from private_methods

I’m new to Ruby, so I probably just don’t understand, but . . .

When I run the following code in Ruby 1.9.2, I receive some unexpected
results.

print BasicObject.private_methods

The returned array includes all the expected methods, but also includes
a few entries I don’t understand. Specifically, the following “methods”
in the returned array are surprising to me.

[…, :Integer, :Float, :String, :Array, …, :Rational, :Complex,
…]

Apparently, I don’t understand something. I thought these were standard
classes. Why do they show up here as methods?

j

its very simple …

Integer is a class, but Integer() is a method that helps you to turn
something into an integer

James D. M. wrote in post #1010480:

[…, :Integer, :Float, :String, :Array, …, :Rational, :Complex,
…]

Apparently, I don’t understand something. I thought these were standard
classes. Why do they show up here as methods?

They are methods in the Kernel module. Method names with a capital
initial are unusual, but allowed.

irb(main):005:0> Kernel.methods.grep(/^[A-Z]/)
=> [“Array”, “Integer”, “Float”, “String”]
irb(main):006:0> Integer(“123”)
=> 123
irb(main):007:0> Integer(“0123”)
=> 83
irb(main):008:0> Integer(“0x123”)
=> 291

Brian C. wrote in post #1010558:

They are methods in the Kernel module.

Aha! And those Kernel methods are the default conversions.

BUT . . .

I thought Kernel was mixed in to Object. Why do these appear as
private_methods of BasicObject?

j

James D. M. wrote in post #1010580:

Brian C. wrote in post #1010558:

They are methods in the Kernel module.

Aha! And those Kernel methods are the default conversions.

BUT . . .

I thought Kernel was mixed in to Object. Why do these appear as
private_methods of BasicObject?

BasicObject is a class right? A class is an instance of Class. And
Class inherits from Object.

The key idea is that almost everything in ruby is an object–including
classes. And all objects inherit from Object.

Things are very “circular” at the top of the ruby object model. Because
BasicObject is an object, it inherits from Object–but Object inherits
from BasicObject. That means BasicObject inherits from Object, which in
turn inherits from BasicObject, which means BasicObject inherits from
itself!