From: "David A. Black" Date: 2008-04-03T15:57:00+09:00 Subject: Re: nested ruby Hi -- On Thu, 3 Apr 2008, Sunil Chikkegowda wrote: > 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 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 -- Rails training from David A. Black and Ruby Power and Light: ADVANCING WITH RAILS April 14-17 New York City INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin See http://www.rubypal.com for details and updates!