# Nested class, require

**URL:** <https://www.ruby-forum.com/t/nested-class-require/50902>\
**Category:** Ruby\
**Created:** [December 22, 2005, 3:19pm UTC](https://www.ruby-forum.com/t/nested-class-require/50902 "2005-12-22T15:19:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 22, 2005, 3:19pm UTC](https://www.ruby-forum.com/t/nested-class-require/50902/1 "2005-12-22T15:19:07Z")

</div>

How does ruby view nested classes - that is, to define a class within  
another class?  
I know that you cannot define a class within a method (not sure why)?

Also, is require the same thing as “copy and paste dynamically”, or does  
it drop down to the global namespace, or do something else?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 22, 2005, 4:53pm UTC](https://www.ruby-forum.com/t/nested-class-require/50902/2 "2005-12-22T16:53:02Z")

</div>

On Thu, 22 Dec 2005 14:19:20 -0000, List R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How does ruby view nested classes - that is, to define a class within  
> another class?  
> I know that you cannot define a class within a method (not sure why)?
> 
> Also, is require the same thing as “copy and paste dynamically”, or does  
> it drop down to the global namespace, or do something else?

I’ve just been playing with this a bit, and have updated the relevant  
nuby  
files:

```
http://roscopeco.co.uk/code/noob/flexi-class.html
http://roscopeco.co.uk/code/noob/require-include.html

```

I’m not sure it answers your question but it exercises these concepts 🙂

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 22, 2005, 5:14pm UTC](https://www.ruby-forum.com/t/nested-class-require/50902/3 "2005-12-22T17:14:06Z")

</div>

List R. wrote:

> is require the same thing as “copy and paste dynamically”, or does  
> it drop down to the global namespace, or do something else?

It’s the second. It’s executed in the context of TOPLEVEL\_BINDING.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [December 23, 2005, 3:16am UTC](https://www.ruby-forum.com/t/nested-class-require/50902/4 "2005-12-23T03:16:29Z")

</div>

List R. wrote:

> How does ruby view nested classes - that is, to define a class within  
> another class?  
> I know that you cannot define a class within a method (not sure why)?

Yeah, you can nest a class within a class. That’s because classes are  
just constants, and classes can contain constants.

class Foo  
def hi; puts ‘hi!’ end  
end  
Bar = Foo  
Bar.new.hi #=\> hi!  
Bar::Bar = Bar  
Bar::Bar.new.hi #=\> hi!  
Bar::Bar::Bar::Bar::Bar::Bar::Bar::Bar.new.hi #=\> hi!

(Mind you, the normal way to do this is just to put the one class  
definition inside the other. Look around for examples of this.)

You _can_ define classes inside methods, just not using the normal  
syntax. This has some consequences, for example on constant and class  
variable scoping, but if you’re dying to dynamically create classes…

def blah  
thing = Class.new {  
def zurr; puts ‘fimmmmmmp’ end  
}  
thing.new.zurr  
end  
blah

To assign this to a constant so you can use it elsewhere, you’re going  
to have to use the magic of const\_set.

> Also, is require the same thing as “copy and paste dynamically”, or does  
> it drop down to the global namespace, or do something else?

Global. I’ve sort of got a hack to allow require’d code to go inside any  
module/class you please, though it’s definitely not for the queasy, and  
it’s not complete yet.

BTW, both of these are things you can test out very easily, using ruby  
or irb at the commandline. Why aren’t you doing that?

Devin
