nested ruby

Discussion in 'Ruby' started by Sunil Chikkegowda, Apr 3, 2008.

  1. 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
    --
    Posted via http://www.ruby-forum.com/.
    Sunil Chikkegowda, Apr 3, 2008
    #1
    1. Advertising

  2. 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!
    David A. Black, Apr 3, 2008
    #2
    1. Advertising

  3. 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/


    On 03/04/2008, at 5:45 PM, 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
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    Julian Leviston, Apr 3, 2008
    #3
  4. 2008/4/3, Sunil Chikkegowda <>:
    > 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
    Robert Klemme, Apr 3, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,081
    Russ Perry Jr
    Aug 20, 2004
  2. Chad E. Dollins
    Replies:
    3
    Views:
    639
    Kai-Uwe Bux
    Nov 8, 2005
  3. request@no_spam.com
    Replies:
    5
    Views:
    407
  4. Ultrus
    Replies:
    3
    Views:
    355
    Stefan Behnel
    Jul 9, 2007
  5. Replies:
    3
    Views:
    450
    Victor Bazarov
    Dec 7, 2007
Loading...

Share This Page