[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Nested class, require

List Recv

12/22/2005 2:19:00 PM

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?

--
Posted via http://www.ruby-....


3 Answers

Ross Bamford

12/22/2005 3:45:00 PM

0

On Thu, 22 Dec 2005 14:19:20 -0000, List Recv <listrecv@gmail.com> 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-...
http://roscopeco.co.uk/code/noob/require-in...

I'm not sure it answers your question but it exercises these concepts :)

--
Ross Bamford - rosco@roscopeco.remove.co.uk

Bob Showalter

12/22/2005 4:14:00 PM

0

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


Devin Mullins

12/23/2005 2:16:00 AM

0

List Recv 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