[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Forward references?

seebs

5/26/2007 12:19:00 PM

In message <3a94cf510705260514u31029d2fr95bb8f96c5520c23@mail.gmail.com>, "Francis Cianfrocca"
writes:
>In this construction you're defining B inside the naming context of A, which
>doesn't exist when the *parser* (which obeys Ruby scoping rules like any
>other Ruby program) needs to use the value of A.

Right. Thus my forward-reference solution:

class A
end

class A::B
...
end

class A
...
end

Which is mostly picked up from C's "struct foo;" forward reference
declarations.

>You see the difference? You don't have to define class A before you define
>class B, but you DO need to *declare* A before you can present it to Ruby's
>parser in a construction like A::xxx.

Right.

>Of course your original question related to style rather than parser
>behavior. You don't say whether you come from a Java background, where the
>relationship between classes and source files is much stricter than it is in
>Ruby.

Mostly C and perl, although I read and write a fair number of languages
now.

>module MyApplication
> class A
> def initialize
> @b = B.new
> end
> end
>end

>#------------------------------

>module MyApplication
> class A
> class B
> end
> end
>end

Makes sense. I'm now doing:

module MyApplication
class A
...
end

class B
...
end
end

because really, I don't have an unambiguous top-level class, so using a module
for namespace is clearer.

-s