# Irb and ruby

**URL:** <https://www.ruby-forum.com/t/irb-and-ruby/74823>\
**Category:** Ruby\
**Created:** [November 18, 2005, 2:34am UTC](https://www.ruby-forum.com/t/irb-and-ruby/74823 "2005-11-18T02:34:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [November 18, 2005, 2:34am UTC](https://www.ruby-forum.com/t/irb-and-ruby/74823/1 "2005-11-18T02:34:39Z")

</div>

Hello all,

I am reading this page  
[http://www.rubyist.net/~slagell/ruby/accesscontrol.html](http://www.rubyist.net/~slagell/ruby/accesscontrol.html)  
especially the “fish”.square example

I am puzzled by different behaviours of ruby itself and the irb

irb(main):101:0\> def square x  
irb(main):102:1\> x\*x  
irb(main):103:1\> end  
=\> nil  
irb(main):105:0\> [].square 12  
=\> 144  
irb(main):106:0\> “fish”.square 12  
=\> 144  
irb(main):112:0\> “fish”.public\_methods.grep /square/  
=\> [“square”]  
irb(main):113:0\> “fish”.private\_methods.grep /square/  
=\> []

I can square my fishes and arrays 😉  
but a ruby program

#!/usr/bin/env ruby  
def square x  
x\*x  
end

puts square 10  
#puts “fish”.square # error, cant square fishes  
puts Object.private\_methods.grep /square/ # yes it’s there  
puts Object.public\_methods.grep /square/ # nope

as you can see, irb makes methods public and ruby private  
what rationale is behind this behaviour?  
is this something that will be “fixed” in future  
or is deliberately dicision/feature

Regards, Daniel

---

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [November 18, 2005, 6:02pm UTC](https://www.ruby-forum.com/t/irb-and-ruby/74823/2 "2005-11-18T18:02:20Z")

</div>

uval wrote:

> Hello all,
> 
> I am reading this page  
> [http://www.rubyist.net/~slagell/ruby/accesscontrol.html](http://www.rubyist.net/~slagell/ruby/accesscontrol.html)  
> especially the “fish”.square example
> 
> I am puzzled by different behaviours of ruby itself and the irb
> 
> irb(main):101:0\> def square x  
> irb(main):102:1\> x\*x  
> irb(main):103:1\> end  
> =\> nil  
> irb(main):105:0\> [].square 12  
> =\> 144  
> irb(main):106:0\> “fish”.square 12  
> =\> 144  
> irb(main):112:0\> “fish”.public\_methods.grep /square/  
> =\> [“square”]  
> irb(main):113:0\> “fish”.private\_methods.grep /square/  
> =\> []
> 
> I can square my fishes and arrays 😉  
> but a ruby program
> 
> #!/usr/bin/env ruby  
> def square x  
> x\*x  
> end
> 
> puts square 10  
> #puts “fish”.square # error, cant square fishes  
> puts Object.private\_methods.grep /square/ # yes it’s there  
> puts Object.public\_methods.grep /square/ # nope
> 
> as you can see, irb makes methods public and ruby private  
> what rationale is behind this behaviour?  
> is this something that will be “fixed” in future  
> or is deliberately dicision/feature
> 
> Regards, Daniel

What’s going on is that when you use irb, you are inside a class called  
“main”; so when you “def square …” you have really created an instance  
method main#square. In pure ruby, your “def square …” creates a  
top-level method.

I do not off the cuff understand why that changes things the way you’re  
seeing, but I think it means that, particularly when using ruby (vs  
irb), you should only “def” inside an explicit class.

---

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [November 18, 2005, 6:13pm UTC](https://www.ruby-forum.com/t/irb-and-ruby/74823/3 "2005-11-18T18:13:56Z")

</div>

Hi –

On Sat, 19 Nov 2005, J. Merrill wrote:

> > irb(main):102:1\> x\*x
> > 
> > puts Object.private\_methods.grep /square/ # yes it’s there  
> > What’s going on is that when you use irb, you are inside a class called  
> > “main”; so when you “def square …” you have really created an instance  
> > method main#square. In pure ruby, your “def square …” creates a  
> > top-level method.

main isn’t a class:

irb(main):021:0\> self  
=\> main  
irb(main):022:0\> self.class  
=\> Object

It’s a default object, a sort of backstop. Also, it’s not  
irb-specific:

$ ruby -e ‘p self; p self.class’  
main  
Object

> I do not off the cuff understand why that changes things the way  
> you’re seeing, but I think it means that, particularly when using  
> ruby (vs irb), you should only “def” inside an explicit class.

I wouldn’t say that. There’s nothing wrong with creating top-level  
methods in Ruby scripts or in irb, if that’s the scope you need them  
in.

I’m not sure why irb doesn’t make top-level methods private, but I  
think that’s the only difference (in this area) between irb and Ruby.

David

---

<div class="post-metadata">

**Author:** ![uval](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/49beb7/32.png) [@uval](https://www.ruby-forum.com/u/uval)\
**Post date:** [November 18, 2005, 6:38pm UTC](https://www.ruby-forum.com/t/irb-and-ruby/74823/4 "2005-11-18T18:38:04Z")

</div>

> > > > > “D” == David A Black [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

D\> I’m not sure why irb doesn’t make top-level methods private, but I  
D\> think that’s the only difference (in this area) between irb and Ruby.

You can change its context

moulon% irb --context-mode 0  
irb(main):001:0\> def aa() 1+1; end  
=\> nil  
irb(main):002:0\> aa  
=\> 2  
irb(main):003:0\> self.aa  
NoMethodError: private method `aa’ called for main:Object  
from (irb):3  
from /usr/lib/ruby/1.8/irb/workspace.rb:27  
from :0  
irb(main):004:0\>  
moulon%

Guy Decoux
