# Self in irb versus script

**URL:** <https://www.ruby-forum.com/t/self-in-irb-versus-script/193137>\
**Category:** Ruby\
**Created:** [August 12, 2010, 2:12am UTC](https://www.ruby-forum.com/t/self-in-irb-versus-script/193137 "2010-08-12T02:12:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 12, 2010, 2:12am UTC](https://www.ruby-forum.com/t/self-in-irb-versus-script/193137/1 "2010-08-12T02:12:29Z")

</div>

Howdy from a Ruby newbie–hope my question isn’t too annoying.

I’m working through a book on Ruby. Examples using self work fine in  
irb, but if I type the same examples in a script and run that I get this  
error message: private method called for…NoMethodError.

So, my question is: what do I need to type to properly invoke self in a  
script (but that is not needed in the irb?).

I should mention that the explanation of public vs. private methods  
isn’t helping me. If I could just see what I have to actually type to  
use self in a script vs. the irb a light would go off in my head.

Here’s an example that runs in irb, but not in a script:

def add(n)  
self + n  
end

puts 2.add(2)

The irb will display the result 4, but the script gives the “private  
method…NoMethodError” message.

I’m puzzled as to why Ullman does not address this in his section on  
“Using Self.”

Thanks!

-Doc Waller

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 13, 2010, 1:17am UTC](https://www.ruby-forum.com/t/self-in-irb-versus-script/193137/2 "2010-08-13T01:17:19Z")

</div>

On 12 Aug 2010, at 01:12, David Waller wrote:

> ## isn’t helping me. If I could just see what I have to actually type to puts 2.add(2) -Doc Waller
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

My irb gave me an error until I added the definition to a class (Integer  
seemed right) :

$ irb

> 2.add(2)  
> NoMethodError: undefined method `add' for 2:Fixnum from (irb):1 from /Library/Frameworks/Ruby.framework/Programs/irb:12:in `’  
> def add(n)  
> self + n  
> end  
> =\> nil  
> 2.add(2)  
> NoMethodError: private method `add' called for 2:Fixnum from (irb):5 from /Library/Frameworks/Ruby.framework/Programs/irb:12:in `’  
> class Integer  
> def add(n)  
> self + n  
> end  
> end  
> =\> nil  
> 2.add(2)  
> =\> 4

I’m not sure why your irb didn’t give an error. Did you load any files  
into it?

Iain

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 13, 2010, 1:46am UTC](https://www.ruby-forum.com/t/self-in-irb-versus-script/193137/3 "2010-08-13T01:46:46Z")

</div>

Iain B. wrote:

> I’m not sure why your irb didn’t give an error. Did you load any files  
> into it?
> 
> Iain

Thanks, Iain. no, I didn’t load any files (that I’m aware of)–and I  
double-checked just now by opening up the terminal fresh and going  
straight to irb and typing it in again–no error.

However, I did find that putting the method into the Integer class as  
you did made the method work properly in a saved script! That moves my  
understanding of self forward a bit. Many thanks for your kind  
response!!

-Dave

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [August 13, 2010, 2:00am UTC](https://www.ruby-forum.com/t/self-in-irb-versus-script/193137/4 "2010-08-13T02:00:43Z")

</div>

Hi –

On Fri, 13 Aug 2010, David Waller wrote:

> double-checked just now by opening up the terminal fresh and going  
> straight to irb and typing it in again–no error.
> 
> However, I did find that putting the method into the Integer class as  
> you did made the method work properly in a saved script! That moves my  
> understanding of self forward a bit. Many thanks for your kind  
> response!!

The difference you’re seeing is a difference in the way irb and the  
non-irb interpreter handle top-level methods. If you define a top-level  
method in a regular Ruby script, it becomes a private instance method of  
Object:

$ ruby -e ‘def x; end; p Object.private\_instance\_methods(false)’  
[“initialize”, “x”]

In irb, such methods become public instance methods of Object:

irb(main):002:0\> def x; end; p Object.public\_instance\_methods(false)  
[“x”]

Private instance methods of Object are known to all objects, but because  
they’re private, they can only be called without an explicit receiver.  
That means that you can’t do:

def x; end  
some\_object.x # error: private method

It’s the same with methods like puts, raise, exit, and sleep. You can  
call:

sleep(3)

but not:

some\_object.sleep(3)

(puts and company are actually private instance methods of Kernel, but  
it’s the same kind of effect.)

The point of all this is to allow Ruby to have these top-level methods,  
while still adhering underneath to the principle that every method call  
is a message being sent to some object.

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

The Ruby training with Black/Brown/McAnally  
Compleat Philadelphia, PA, October 1-2, 2010  
Rubyist [http://www.compleatrubyist.com](http://www.compleatrubyist.com)
