# Ruby Basics / define met;hod

**URL:** <https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154>\
**Category:** Ruby\
**Created:** [March 6, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154 "2008-03-06T14:37:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 6, 2008, 2:37pm UTC](https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154/1 "2008-03-06T14:37:53Z")

</div>

Hi,  
I am new to Ruby and This is a very basic question.when I type p self in  
irb it gives me ‘main’.  
when I say class what is happening in the backend.since within a class  
end I am able to call define\_method which is a private method and should  
never be available to anybody and another intersting thing is it is not  
accessible outside of the  
class.  
so my question is how is a private method accessible outside  
what is happening when a class is defined. it is of type Class  
automatically…I don’t quite get it.  
As soon as you start IRB a main object is created which is of type  
Object.is it is right statement…  
THnks  
Chinna

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 6, 2008, 3:13pm UTC](https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154/2 "2008-03-06T15:13:31Z")

</div>

On Thu, Mar 6, 2008 at 2:37 PM, Chinna K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> I am new to Ruby and This is a very basic question.when I type p self in  
> irb it gives me ‘main’.

I can’t say much to the details, how irb is implemented, but you can  
overwrite what “p object” returns, when you overwrite the inspect()  
method:  
class FooBar  
end  
p FooBar.new #=\> #FooBar:0x2aa50f8

class FooBar  
def inspect  
“I am FooBar”  
end  
end  
p FooBar.new #=\> I am FooBar

> when I say class what is happening in the backend.since within a class  
> end I am able to call define\_method which is a private method and should  
> never be available to anybody and another intersting thing is it is not  
> accessible outside of the  
> class.

You can use the send method to call private methods in Ruby 1.8.6, I  
am not sure if it works in 1.9  
class Baz  
private  
def hello; “HELLO!”; end  
end

Baz.new.send(:hello) #=\> “HELLO!”

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 6, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154/3 "2008-03-06T15:44:22Z")

</div>

2008/3/6, Chinna K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am new to Ruby and This is a very basic question.when I type p self in  
> irb it gives me ‘main’.

Correct.

> when I say class what is happening in the backend.since within a class  
> end I am able to call define\_method which is a private method and should  
> never be available to anybody and another intersting thing is it is not  
> accessible outside of the class.

irb(main):001:0\> class Foo  
irb(main):002:1\> p [self,self.class]  
irb(main):003:1\> end  
[Foo, Class]  
=\> nil  
irb(main):004:0\>

Inside a class body /self/ is the class instance being defined right  
now.

> so my question is how is a private method accessible outside  
> what is happening when a class is defined.

First of all, inside “class…end” you are not “outside”.

Second, if you want to access any private method you can do so via send:

irb(main):004:0\> class Foo  
irb(main):005:1\> private  
irb(main):006:1\> def bar()123 end  
irb(main):007:1\> end  
=\> nil  
irb(main):008:0\> Foo.new.bar  
NoMethodError: private method `bar’ called for #Foo:0x7ff82440  
from (irb):8  
from :0  
irb(main):009:0\> Foo.new.send :bar  
=\> 123

> it is of type Class  
> automatically…I don’t quite get it.  
> As soon as you start IRB a main object is created which is of type  
> Object.is it is right statement…

I do not know what you mean by your last sentence. “main” is of type  
Object - but it has some methods added:

$ irb  
irb(main):001:0\> puts class\<\<self;self;end.instance\_methods(false).sort  
bindings  
cb  
chws  
conf  
context  
cws  
cwws  
exit  
fg  
help  
include  
install\_alias\_method  
irb  
irb\_bindings  
irb\_cb  
irb\_change\_binding  
irb\_change\_workspace  
irb\_chws  
irb\_context  
irb\_current\_working\_binding  
irb\_current\_working\_workspace  
irb\_cwb  
irb\_cws  
irb\_cwws  
irb\_exit  
irb\_fg  
irb\_help  
irb\_jobs  
irb\_kill  
irb\_load  
irb\_pop\_binding  
irb\_pop\_workspace  
irb\_popb  
irb\_popws  
irb\_print\_working\_binding  
irb\_print\_working\_workspace  
irb\_push\_binding  
irb\_push\_workspace  
irb\_pushb  
irb\_pushws  
irb\_pwb  
irb\_pwws  
irb\_quit  
irb\_require  
irb\_source  
irb\_workspaces  
jobs  
kill  
popb  
popws  
private  
public  
pushb  
pushws  
pwws  
quit  
source  
to\_s  
workspaces  
=\> nil  
irb(main):002:0\>

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 6, 2008, 4:47pm UTC](https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154/4 "2008-03-06T16:47:02Z")

</div>

2008/3/6, Chinna K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Thanks both of you…  
> When I say inside of class…end  
> class A  
> define\_method(:name) { p self }  
> end  
> A.new.name will give me the self…if define\_method is a private method  
> how is it I am able to access from inside the class …end

Please read my reply.

robert

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 6, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154/5 "2008-03-06T17:18:51Z")

</div>

I did read through your reply.especially this…  
“Inside a class body /self/ is the class instance being defined right  
now.”  
I still could not understand.  
BTW even this is little foreign to me  
puts class\<\<self;self;end.instance\_methods(false).sort

is class an array that you are appending the self array and after that  
you end.instance\_methods  
Can you explain it in a little black & white…  
THnks  
chinna

Robert K. wrote:

> 2008/3/6, Chinna K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Thanks both of you…  
> > When I say inside of class…end  
> > class A  
> > define\_method(:name) { p self }  
> > end  
> > A.new.name will give me the self…if define\_method is a private method  
> > how is it I am able to access from inside the class …end
> 
> Please read my reply.
> 
> robert

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 6, 2008, 4:06pm UTC](https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154/6 "2008-03-06T16:06:40Z")

</div>

Thanks both of you…  
When I say inside of class…end  
class A  
define\_method(:name) { p self }  
end  
A.new.name will give me the self…if define\_method is a private method  
how is it I am able to access from inside the class …end

THnks  
ManickamPR.

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 6, 2008, 6:00pm UTC](https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154/7 "2008-03-06T18:00:59Z")

</div>

Chinna K. wrote:

> I did read through your reply.especially this…  
> “Inside a class body self is the class instance being defined right  
> now.”  
> I still could not understand.  
> BTW even this is little foreign to me  
> puts class\<\<self;self;end.instance\_methods(false).sort
> 
> is class an array that you are appending the self array and after that  
> you end.instance\_methods

what he did is:

irb(main):005:0\> a =  
irb(main):006:0\* class\<\<self  
irb(main):007:1\> self  
irb(main):008:1\> end  
=\> #\<Class:#Object:0xb7cf29a4\>

so now in a you have a class object. self is the current instance of the  
class.  
then with  
a.instance\_methods(false)  
you can print all instance methods of self. the .sort only sorts them  
alphabetically.

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 6, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/ruby-basics-define-met-hod/131154/8 "2008-03-06T18:50:51Z")

</div>

On 06.03.2008 17:18, Chinna K. wrote:

> I did read through your reply.especially this…  
> “Inside a class body /self/ is the class instance being defined right  
> now.”  
> I still could not understand.

You wondered why define\_method can be invoked inside class…end because  
it is private and should not be accessible “from outside”. Private  
methods can normally be only invoked without an explicit receiver, i.e.  
only on /self/. I explained and demonstrated (via the printing) that  
inside class…end /self/ is actually the class being defined. Hence  
you can invoke define\_method inside class…end. q.e.d.

Then I went on to explain that /private/ in Ruby is rather weak because  
you can invoke private methods on any instance simply by going through  
/send/.

Cheers

robert
