# Top-level methods and constants

**URL:** <https://www.ruby-forum.com/t/top-level-methods-and-constants/119801>\
**Category:** Ruby\
**Created:** [November 10, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/top-level-methods-and-constants/119801 "2007-11-10T18:11:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [November 10, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/top-level-methods-and-constants/119801/1 "2007-11-10T18:11:29Z")

</div>

A couple of questions regarding the top-level environment of execution.

- The Pickaxe says on page 376 that top-level methods are defined as  
private instance methods of Object, whereas Ruby for Rails says on  
page 203 they are private instance methods of the Kernel module. Which  
one is correct?

- Which is the actual container of a top-level constant?

– fxn

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [November 10, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/top-level-methods-and-constants/119801/2 "2007-11-10T18:25:31Z")

</div>

Hi –

On Sun, 11 Nov 2007, Xavier N. wrote:

> A couple of questions regarding the top-level environment of execution.
> 
> - The Pickaxe says on page 376 that top-level methods are defined as private  
> instance methods of Object, whereas Ruby for Rails says on page 203 they are  
> private instance methods of the Kernel module. Which one is correct?

The Pickaxe is right. I believe it got fixed in later printings of  
R4R, though. I can’t remember what led to the original mistake – I  
think I misinterpreted some method-list output I was getting while  
examining it all in irb, or something.

> - Which is the actual container of a top-level constant?

You may not want me to answer 🙂 But I do believe it’s Object.

ruby -ve “A=1; Object::A; Kernel::A”  
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.1]  
-e:1: uninitialized constant Kernel::A (NameError)

(plus a couple of void context warnings which don’t matter)

David

---

<div class="post-metadata">

**Author:** ![fxn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/77aa72/32.png) [@fxn](https://www.ruby-forum.com/u/fxn)\
**Post date:** [November 10, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/top-level-methods-and-constants/119801/3 "2007-11-10T18:29:36Z")

</div>

On Nov 10, 2007 12:10 PM, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> A couple of questions regarding the top-level environment of execution.
> 
> - The Pickaxe says on page 376 that top-level methods are defined as  
> private instance methods of Object, whereas Ruby for Rails says on  
> page 203 they are private instance methods of the Kernel module. Which  
> one is correct?

Looks like the pickaxe:

def foo  
end

self.private\_methods.include?(“foo”) # =\> true  
Object.private\_instance\_methods(false).include?(“foo”) # =\> true  
Kernel.private\_instance\_methods(false).include?(“foo”) # =\> false

Note though that in irb top level methods are public:  
irb(main):001:0\> def foo  
irb(main):002:1\> end  
=\> nil  
irb(main):003:0\>  
irb(main):004:0\* self.private\_methods.include?(“foo”)  
=\> false  
irb(main):005:0\> Object.private\_instance\_methods(false).include?(“foo”)  
=\> false  
irb(main):006:0\> Kernel.private\_instance\_methods(false).include?(“foo”)  
=\> false  
irb(main):007:0\> Object.instance\_methods(false).include?(“foo”)  
=\> true  
irb(main):008:0\>

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
