# Getting methods defined in the top scope

**URL:** <https://www.ruby-forum.com/t/getting-methods-defined-in-the-top-scope/119917>\
**Category:** Ruby\
**Created:** [November 12, 2007, 6:53am UTC](https://www.ruby-forum.com/t/getting-methods-defined-in-the-top-scope/119917 "2007-11-12T06:53:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 12, 2007, 6:53am UTC](https://www.ruby-forum.com/t/getting-methods-defined-in-the-top-scope/119917/1 "2007-11-12T06:53:26Z")

</div>

I am trying to get the names of the methods defined by myself in the top  
scope. I tried it by using  
" self.class.private\_instance\_methods(false).sort "

But it gives two additional methods given below.  
inherited  
initialize

but i want to get only the methods defined by my self. Is there any way  
to solve this ?

thanks!

regards,  
buddhika

---

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 12, 2007, 7:27am UTC](https://www.ruby-forum.com/t/getting-methods-defined-in-the-top-scope/119917/2 "2007-11-12T07:27:19Z")

</div>

Thilina B. wrote:

> I am trying to get the names of the methods defined by myself in the top  
> scope. I tried it by using  
> " self.class.private\_instance\_methods(false).sort "
> 
> But it gives two additional methods given below.  
> inherited  
> initialize
> 
> but i want to get only the methods defined by my self. Is there any way  
> to solve this ?
> 
> thanks!
> 
> regards,  
> buddhika

def hello  
end

def goodbye  
end

class Dog  
end

value = 10

#---------

p Object.private\_instance\_methods(false)  
—\>[“goodbye”, “initialize”, “hello”] #No inherited method anywhere.

method\_names = Object.private\_instance\_methods(false)  
my\_methods = []

method\_names.each do |meth\_name|  
if meth\_name == ‘initialize’ or meth\_name == ‘inherited’  
next  
end

my\_methods \<\< meth\_name  
end

p my\_methods  
–\>[“goodbye”, “hello”]

---

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 12, 2007, 7:48am UTC](https://www.ruby-forum.com/t/getting-methods-defined-in-the-top-scope/119917/3 "2007-11-12T07:48:23Z")

</div>

Thilina B. wrote:

> I am trying to get the names of the methods defined by myself in the top  
> scope. I tried it by using  
> " self.class.private\_instance\_methods(false).sort "
> 
> But it gives two additional methods given below.  
> inherited  
> initialize
> 
> but i want to get only the methods defined by my self. Is there any way  
> to solve this ?
> 
> thanks!
> 
> regards,  
> buddhika

In the same line of thought as previous response, but more concise:

not\_mine = %w[initialize …]

my\_methods = Object.private\_instance\_methods(false) - not\_mine

---

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 12, 2007, 8:44am UTC](https://www.ruby-forum.com/t/getting-methods-defined-in-the-top-scope/119917/4 "2007-11-12T08:44:49Z")

</div>

thanks guys!

Regards,  
buddhika

---

<div class="post-metadata">

**Author:** ![thilinamb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@thilinamb](https://www.ruby-forum.com/u/thilinamb)\
**Post date:** [November 12, 2007, 8:34am UTC](https://www.ruby-forum.com/t/getting-methods-defined-in-the-top-scope/119917/5 "2007-11-12T08:34:15Z")

</div>

On Nov 12, 2007 4:53 PM, Thilina B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but i want to get only the methods defined by my self. Is there any way  
> to solve this ?

This gives the same result as the others, but avoids hardcoding method  
names:

$ cat toplevel\_methods.rb  
TOPLEVEL\_METHODS = []

def Object.method\_added(name)  
TOPLEVEL\_METHODS \<\< name if private\_method\_defined?(name)  
end

def toplevel  
end

class Object  
def not\_toplevel  
end

private  
def fake\_toplevel  
end  
end

p TOPLEVEL\_METHODS

$ ruby toplevel\_methods.rb  
[:toplevel, :fake\_toplevel]
