Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a nested class access a method in the outer class in Ruby?

Tags:

def class A   def a     raise "hi" #can't be reached   end    class B     def b       a() #doesn't find method a.     end   end end 

I want to invoke a from b and raise the exception. How can I?

like image 694
nes1983 Avatar asked Feb 04 '11 22:02

nes1983


People also ask

Can inner class access outer class method?

Method Local inner classes can't use a local variable of the outer method until that local variable is not declared as final. For example, the following code generates a compiler error.

How does an inner class instance access the outer class members?

If you want your inner class to access outer class instance variables then in the constructor for the inner class, include an argument that is a reference to the outer class instance. The outer class invokes the inner class constructor passing this as that argument.

Can nested class inherit outer class?

A static nested class can inherit: an ordinary class. a static nested class that is declared in an outer class or its ancestors.

How do you access the variables of an outer class?

You can access the static variable of an outer class just using the class name.


1 Answers

Ruby doesn't have nested classes.

The only way to inherit behavior is, well, via inheritance.

If you want your code to work, you need to use a language which supports nested classes. While this is an incredibly neat and powerful feature, I unfortunately know of only two languages that have nested classes:

  • BETA, the language which introduced nested classes (and its successor gbeta)
  • Newspeak

I don't know of any other.

Java has a construct called nested classes, but they have some unfortunate design limitations.

In your example above, it's not the class B that is nested inside A, it is the constant B that is nested inside A. Think about this:

C = A::B 

Now, the class is available under two names: A::B and C. It should be immediately obvious that C is global and not nested inside A. (Well, actually, C is nested inside Object, because there aren't really global constants either, but that's beside the point.) But since C and A::B are the same class, it obviously cannot be both nested and not nested. The only logical conclusion is that the class itself isn't nested.

The defining feature of nested classes is that method lookup goes along two dimensions: up the inheritance chain, and outwards through the nesting. Ruby, like 99.9% of all OO languages, only supports the former. (In some sense, nested classes inherit not only the features of their superclass, but also the features of their surrounding class.)

like image 125
Jörg W Mittag Avatar answered Sep 28 '22 04:09

Jörg W Mittag