From: Richard Everhart Date: 2008-02-01T07:23:16+09:00 Subject: Re: Referring to method in enclosing class Thanks for the reply. I wanted to add functionality to the enclosing class via the addition of the enclosed class with minimal changes to the enclosing class and its methods. Specifically, the enclosed class is intended to subclass a threading framework class and, after instantiated, call methods on the enclosing class. The enclosing class already extends another class. I come from a Java background if that helps explaining anything about my approach to this. Rich Gary Wright wrote: > On Jan 30, 2008, at 8:42 PM, Richard Everhart wrote: > >> >> end > This doesn't really make sense. First of all nested classes are just > a way to organize the *names* of classes. The nesting is completely > orthogonal to the inheritance hierarchy. In your example, > Enclosing and Enclosing::Hello are only related by their name. > Neither one is a subclass or parent of the other, they are both > subclasses of Object. > > The second issue is that those are instance methods so you'll need an > instance of Enclosing and and instance of Hello before you can even > think of calling Hello#do_say_hello or Enclosing#say_hello. > > I'm not sure what you are after but you kind of have two choices. > Make Hello a subclass of Enclosing, or make Enclosing a module and > include its methods into Hello. These examples below will 'work' > but they are awkward at best. I'm not sure exactly what you are > trying to do so I'm not sure what to suggest instead. > > > Subclass: > > class Enclosing > def go > Hello.new.do_say_hello > end > > def say_hello > "Hello World!" > end > > class Hello < Enclosing > def do_say_hello > puts say_hello > end > end > > end > > Enclosing.new.go > > > # Module > > module Enclosing > def go > Hello.new.do_say_hello > end > > def say_hello > "Hello World!" > end > > class Hello > include Enclosing > def do_say_hello > puts say_hello > end > end > > end > > Enclosing::Hello.new.go -- Posted via http://www.ruby-forum.com/.