Problem with inner classes

I’m having a problem defining inner classes. The outer class is a
child of another class, which makes reusing the name for defining
inner classes a little cumbersome.

For example, if the outer class is:

class OuterClass < AbstractOuterClass

end

and in InnerClass.rb I have:

class OuterClass
class InnerClass

end
end

I get the following error:

warning: already initialized constant OuterClass

… which I’m assuming comes from the fact that the interpreter
thinks I want to redefine OuterClass as having no parent. If I change
my InnerClass code to

class OuterClass < AbstractOuterClass
class InnerClass

end
end

… then it works. Which is fine, except I have maybe 20 inner
classes and I’d rather not duplicate the OuterClass inheritance in
each file.

Any ways I can get around this?

Thanks,

Francis

Hi,

···

In message “problem with inner classes” on 02/06/18, Francis Hwang sera@fhwang.net writes:

For example, if the outer class is:

class OuterClass < AbstractOuterClass

end

and in InnerClass.rb I have:

class OuterClass
class InnerClass

end
end

I get the following error:

warning: already initialized constant OuterClass

Which version of Ruby are you using? You will get no error on 1.6.7
or 1.7.2

						matz.

I’m curious why you have that many “inner” classes. Mind sharing?

If I had 20 of them and they were substantial enough to deserve their own
files, I think I would have put them inside a module instead of inside a
class. At least in the scenarios I can think of.

k

“Francis Hwang” sera@fhwang.net wrote in message
news:a05100303b933cd300ae6@[10.0.1.6]…

···

I’m having a problem defining inner classes. The outer class is a
child of another class, which makes reusing the name for defining
inner classes a little cumbersome.

For example, if the outer class is:

class OuterClass < AbstractOuterClass

end

and in InnerClass.rb I have:

class OuterClass
class InnerClass

end
end

I get the following error:

warning: already initialized constant OuterClass

… which I’m assuming comes from the fact that the interpreter
thinks I want to redefine OuterClass as having no parent. If I change
my InnerClass code to

class OuterClass < AbstractOuterClass
class InnerClass

end
end

… then it works. Which is fine, except I have maybe 20 inner
classes and I’d rather not duplicate the OuterClass inheritance in
each file.

Any ways I can get around this?

Thanks,

Francis

This (of course) works:
class Abstract
end
class Outer
end
class Outer < Abstract
end

I think he wants this to work too:
class Abstract
end
class Outer < Abstract
end
class Outer
end

That working would suprise me, personally.

k

“Yukihiro Matsumoto” matz@ruby-lang.org wrote in message
news:1024411192.423217.7543.nullmailer@picachu.netlab.jp…

···

Hi,

In message “problem with inner classes” > on 02/06/18, Francis Hwang sera@fhwang.net writes:

For example, if the outer class is:

class OuterClass < AbstractOuterClass

end

and in InnerClass.rb I have:

class OuterClass
class InnerClass

end
end

I get the following error:

warning: already initialized constant OuterClass

Which version of Ruby are you using? You will get no error on 1.6.7
or 1.7.2

matz.

Hi,

···

In message “Re: problem with inner classes” on 02/06/20, “k” kturing@yahoo.com writes:

This (of course) works:
class Abstract
end
class Outer
end
class Outer < Abstract
end

I think he wants this to work too:
class Abstract
end
class Outer < Abstract
end
class Outer
end

That working would suprise me, personally.

I think you meant the former does not work, the latter does.
When re-defining a class, a class statement should be either

  • no superclass specified
  • same superclass specified

It’s the reason that the former did not work.

						matz.