# Referring to method in enclosing class

**URL:** <https://www.ruby-forum.com/t/referring-to-method-in-enclosing-class/127652>\
**Category:** Ruby\
**Created:** [January 31, 2008, 2:42am UTC](https://www.ruby-forum.com/t/referring-to-method-in-enclosing-class/127652 "2008-01-31T02:42:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![richever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@richever](https://www.ruby-forum.com/u/richever)\
**Post date:** [January 31, 2008, 2:42am UTC](https://www.ruby-forum.com/t/referring-to-method-in-enclosing-class/127652/1 "2008-01-31T02:42:46Z")

</div>

I need to do something like what is being attempted in the code below.

The commented out code is what I can’t figure out: how do methods in a  
nested classes call methods in their enclosing class? Is this even  
possible?

class Enclosing  
def go  
Hello.new.do\_say\_hello  
end

def say\_hello  
“Hello World!”  
end

class Hello  
def do\_say\_hello

# puts Enclosing class’s say\_hello method

```
end

```

end

end

---

<div class="post-metadata">

**Author:** ![richever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@richever](https://www.ruby-forum.com/u/richever)\
**Post date:** [January 31, 2008, 5:25am UTC](https://www.ruby-forum.com/t/referring-to-method-in-enclosing-class/127652/2 "2008-01-31T05:25:55Z")

</div>

On Jan 30, 2008, at 8:42 PM, Richard E. 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

---

<div class="post-metadata">

**Author:** ![richever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@richever](https://www.ruby-forum.com/u/richever)\
**Post date:** [January 31, 2008, 8:56am UTC](https://www.ruby-forum.com/t/referring-to-method-in-enclosing-class/127652/3 "2008-01-31T08:56:50Z")

</div>

Richard E. wrote:

> I need to do something like what is being attempted in the code below.
> 
> The commented out code is what I can’t figure out: how do methods in a  
> nested classes call methods in their enclosing class? Is this even  
> possible?
> 
> class Enclosing  
> def go  
> Hello.new.do\_say\_hello  
> end
> 
> def say\_hello  
> “Hello World!”  
> end
> 
> class Hello  
> def do\_say\_hello
> 
> # puts Enclosing class’s say\_hello method
> 
> ```
> end
> 
> ```
> 
> end
> 
> end

Why do you want to nest Hello inside Enclosing?

class Enclosing  
def say\_hello  
puts “hi”  
end  
end

class Hello  
def initialize  
@e = Enclosing.new  
end

def do\_say\_hello  
@e.say\_hello  
end  
end

h = Hello.new  
h.do\_say\_hello

–output:–  
hi

---

<div class="post-metadata">

**Author:** ![richever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@richever](https://www.ruby-forum.com/u/richever)\
**Post date:** [January 31, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/referring-to-method-in-enclosing-class/127652/4 "2008-01-31T23:23:22Z")

</div>

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 W. wrote:

> On Jan 30, 2008, at 8:42 PM, Richard E. 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

---

<div class="post-metadata">

**Author:** ![richever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@richever](https://www.ruby-forum.com/u/richever)\
**Post date:** [January 31, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/referring-to-method-in-enclosing-class/127652/5 "2008-01-31T23:34:11Z")

</div>

Richard E. wrote:

> I need to do something like what is being attempted in the code below.
> 
> The commented out code is what I can’t figure out: how do methods in a  
> nested classes call methods in their enclosing class? Is this even  
> possible?
> 
> class Enclosing  
> def go  
> Hello.new.do\_say\_hello  
> end
> 
> def say\_hello  
> “Hello World!”  
> end
> 
> class Hello  
> def do\_say\_hello
> 
> # puts Enclosing class’s say\_hello method
> 
> ```
> end
> 
> ```
> 
> end
> 
> end

Not to fear, I have used similar stuff in other languages … I believe  
what you are looking for is delegates:

[http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html](http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html)

hth

ilan

---

<div class="post-metadata">

**Author:** ![richever](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/51bf81/32.png) [@richever](https://www.ruby-forum.com/u/richever)\
**Post date:** [February 1, 2008, 12:05am UTC](https://www.ruby-forum.com/t/referring-to-method-in-enclosing-class/127652/6 "2008-02-01T00:05:58Z")

</div>

On Jan 31, 3:23 pm, Richard E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

class Foo  
def yay  
puts “foo sez yay”  
end

# Creates a Foo::Bar class that is a subclass of Foo

class Bar \< Foo  
def yay  
puts “bar sez yay”  
super  
puts “bar iz done”  
end  
end  
end

Foo::Bar.new.yay  
#=\> bar sez yay  
#=\> foo sez yay  
#=\> bar iz done
