# Nested ruby

**URL:** <https://www.ruby-forum.com/t/nested-ruby/133750>\
**Category:** Ruby\
**Created:** [April 3, 2008, 8:45am UTC](https://www.ruby-forum.com/t/nested-ruby/133750 "2008-04-03T08:45:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [April 3, 2008, 8:45am UTC](https://www.ruby-forum.com/t/nested-ruby/133750/1 "2008-04-03T08:45:32Z")

</div>

Hello All,

Take a look at this example:

def method1  
puts ‘hello’  
def method2  
puts ‘world’  
end  
end

In IRB, if I say ‘method1’ it gives ‘hello’ as output. For  
‘method1.method2’ it gives ‘hello’ in first line and ‘world’ in second  
line.

My question is: Is there any way to print only ‘world’ if I execute  
method1.method2

---

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [April 3, 2008, 8:57am UTC](https://www.ruby-forum.com/t/nested-ruby/133750/2 "2008-04-03T08:57:45Z")

</div>

Hi –

On Thu, 3 Apr 2008, Sunil C. wrote:

> In IRB, if I say ‘method1’ it gives ‘hello’ as output. For  
> ‘method1.method2’ it gives ‘hello’ in first line and ‘world’ in second  
> line.
> 
> My question is: Is there any way to print only ‘world’ if I execute  
> method1.method2

Given the above definition, you can’t execute method1 without having  
it print ‘hello’. The fact that method2 prints ‘world’ is totally  
unconnected – in fact, method1 and method2 are unconnected. When you  
do method1.method2, you’re actually calling method2 on nil (the return  
value of method1).

You can see this from a slightly different angle here:

irb(main):007:0\> def nil.m2  
irb(main):008:1\> “Calling nil.m2”  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> def m1  
irb(main):011:1\> def m2  
irb(main):012:2\> super  
irb(main):013:2\> end  
irb(main):014:1\> end  
=\> nil  
irb(main):015:0\> m1.m2  
=\> “Calling nil.m2”

You can tell that the call to m2 on line 15 has nil as its receiver,  
because it successfully finds the “super” version that was defined on  
line 7.

David

---

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [April 3, 2008, 3:29pm UTC](https://www.ruby-forum.com/t/nested-ruby/133750/3 "2008-04-03T15:29:58Z")

</div>

2008/4/3, Sunil C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> In IRB, if I say ‘method1’ it gives ‘hello’ as output. For  
> ‘method1.method2’ it gives ‘hello’ in first line and ‘world’ in second  
> line.
> 
> My question is: Is there any way to print only ‘world’ if I execute  
> method1.method2

You seem to misunderstand what method1.method2 does: it is not an  
access to a nested method definition (because those do not exist in  
Ruby) - rather two methods are called one after the other. If you  
only want method2 you can do it:

irb(main):001:0\> def method1  
irb(main):002:1\> puts ‘hello’  
irb(main):003:1\> def method2  
irb(main):004:2\> puts ‘world’  
irb(main):005:2\> end  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> method2  
NameError: undefined local variable or method `method2’ for main:Object  
from (irb):7  
from :0  
irb(main):008:0\> method1  
hello  
=\> nil  
irb(main):009:0\> method2  
world  
=\> nil  
irb(main):010:0\>

But in your example there is no point in defining method2 via  
executing method1 - at least I cannot see any.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [April 3, 2008, 9:19am UTC](https://www.ruby-forum.com/t/nested-ruby/133750/4 "2008-04-03T09:19:42Z")

</div>

That’s a really odd example, Sunil.

What are you trying to accomplish?

By chaining two methods together, you’re calling them both, and ruby  
is providing you with just this response.

if you want to group methods, then you should most likely use a class  
or a module.

Let us know your requirements a little more succinctly and we’ll try  
to provide a nice solution to your problem.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO  
(#2) OUT NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
