Robert Klemme
4/3/2008 1:29:00 PM
2008/4/3, Sunil Chikkegowda <ycsunil@gmail.com>:
> 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
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
--
use.inject do |as, often| as.you_can - without end