Nested ruby

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

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

2008/4/3, Sunil C. [email protected]:

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

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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs