# Methods defined in main

**URL:** <https://www.ruby-forum.com/t/methods-defined-in-main/99941>\
**Category:** Ruby\
**Created:** [February 1, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/methods-defined-in-main/99941 "2006-02-01T23:00:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [February 1, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/methods-defined-in-main/99941/1 "2006-02-01T23:00:41Z")

</div>

Hello,  
I understand that everything in ruby is an object so a method defined  
in a file like this in a file

def meth1  
puts self  
end

actually prints main.  
but when I run it in the debugger mode and do main.methods. I get  
‘main’ undefined variable…but If i do self.methods I can see the  
methods but cant see the meth1 method.I thought ruby opens the ‘main’  
object and adds methods to that instance.  
So whats happening here?  
Vivek

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [February 1, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/methods-defined-in-main/99941/2 "2006-02-01T23:06:46Z")

</div>

Vivek wrote:

> but when I run it in the debugger mode and do main.methods. I get  
> ‘main’ undefined variable…

The fact that self when printed shows “main” does not mean that there is  
a  
variable with that name. You can make anything print main:

15:57:02 [~]: ruby -e ‘o=Object.new; def o.to\_s() “main” end; puts o’  
main

> but If i do self.methods I can see the  
> methods but cant see the meth1 method.I thought ruby opens the ‘main’  
> object and adds methods to that instance.  
> So whats happening here?

You don’t get to see private methods - and methods defined on top level  
are implicitely private:

15:56:46 [~]: ruby -e ‘def foo() puts “x” end; self.foo()’  
-e:1: private method `foo’ called for main:Object (NoMethodError)

15:56:58 [~]: ruby -e ‘def foo() puts “x” end; foo()’  
x

15:54:57 [~]: ruby -e ‘def foo() end; p  
self.private\_methods.grep(/foo/)’  
[“foo”]  
15:57:44 [~]: ruby -e ‘def foo() end; p private\_methods.grep(/foo/)’  
[“foo”]

KInd regards

```
robert
```
