[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to avoid multiple inclusion of a file

suresh

6/11/2008 4:22:00 PM

Hi

My program is split up into multiple files, each containing a set of
related class definitions. In the main file, all these files are
included by using require. But how can i avoid multiple inclusion of a
file by mistake.

Is there anything similar to C languages #ifndef .......

thanks
suresh
5 Answers

Jason Roelofs

6/11/2008 4:29:00 PM

0

As long as the require lines are exactly the same, there's nothing to
worry about.

file1.rb:
require 'lib'

file2.rb:
require 'lib'

This will work fine. You will run into problems if you put relative
paths though:

file1.rb:
require 'lib'

lower/file3.rb:
require '../lib'

This will end up require-ing lib.rb twice, and possibly cause problems.

Either use full paths, or add directories to the $LOAD_PATH to protect
against this.

Jason

On Wed, Jun 11, 2008 at 12:24 PM, suresh <suresh.amritapuri@gmail.com> wrote:
> Hi
>
> My program is split up into multiple files, each containing a set of
> related class definitions. In the main file, all these files are
> included by using require. But how can i avoid multiple inclusion of a
> file by mistake.
>
> Is there anything similar to C languages #ifndef .......
>
> thanks
> suresh
>
>

ara.t.howard

6/11/2008 4:34:00 PM

0


On Jun 11, 2008, at 10:24 AM, suresh wrote:

> Is there anything similar to C languages #ifndef .......


unless defined? A

require 'a'

end



a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




suresh

6/11/2008 4:34:00 PM

0

On Jun 11, 9:28 pm, Jason Roelofs <jameskil...@gmail.com> wrote:
> As long as the require lines are exactly the same, there's nothing to
> worry about.
>
> file1.rb:
> require 'lib'
>
> file2.rb:
> require 'lib'
>
> This will work fine. You will run into problems if you put relative
> paths though:
>
> file1.rb:
> require 'lib'
>
> lower/file3.rb:
> require '../lib'
>
> This will end up require-ing lib.rb twice, and possibly cause problems.
>
> Either use full paths, or add directories to the $LOAD_PATH to protect
> against this.
>
> Jason
>
> On Wed, Jun 11, 2008 at 12:24 PM, suresh <suresh.amritap...@gmail.com> wrote:
> > Hi
>
> > My program is split up into multiple files, each containing a set of
> > related class definitions. In the main file, all these files are
> > included by using require. But how can i avoid multiple inclusion of a
> > file by mistake.
>
> > Is there anything similar to C languages #ifndef .......
>
> > thanks
> > suresh

Hi Jason,

Will tell you what happened to me. I had some database populating code
in some import class definitions. By mistake that file got included
twice and I had to spend a lot of time to figure out how this
happened. So thats why my question, how can I avoid multiple inclusion
of files.

suresh

Jason Roelofs

6/11/2008 4:40:00 PM

0

On Wed, Jun 11, 2008 at 12:34 PM, suresh <suresh.amritapuri@gmail.com> wrote:
> On Jun 11, 9:28 pm, Jason Roelofs <jameskil...@gmail.com> wrote:
>> As long as the require lines are exactly the same, there's nothing to
>> worry about.
>>
>> file1.rb:
>> require 'lib'
>>
>> file2.rb:
>> require 'lib'
>>
>> This will work fine. You will run into problems if you put relative
>> paths though:
>>
>> file1.rb:
>> require 'lib'
>>
>> lower/file3.rb:
>> require '../lib'
>>
>> This will end up require-ing lib.rb twice, and possibly cause problems.
>>
>> Either use full paths, or add directories to the $LOAD_PATH to protect
>> against this.
>>
>> Jason
>>
>> On Wed, Jun 11, 2008 at 12:24 PM, suresh <suresh.amritap...@gmail.com> wrote:
>> > Hi
>>
>> > My program is split up into multiple files, each containing a set of
>> > related class definitions. In the main file, all these files are
>> > included by using require. But how can i avoid multiple inclusion of a
>> > file by mistake.
>>
>> > Is there anything similar to C languages #ifndef .......
>>
>> > thanks
>> > suresh
>
> Hi Jason,
>
> Will tell you what happened to me. I had some database populating code
> in some import class definitions. By mistake that file got included
> twice and I had to spend a lot of time to figure out how this
> happened. So thats why my question, how can I avoid multiple inclusion
> of files.
>
> suresh
>
>

I just told you how to avoid it. If you're getting collisions, then
you're doing what I've outlined above.

Jason

ara.t.howard

6/11/2008 4:47:00 PM

0


On Jun 11, 2008, at 10:34 AM, suresh wrote:

> Hi Jason,
>
> Will tell you what happened to me. I had some database populating code
> in some import class definitions. By mistake that file got included
> twice and I had to spend a lot of time to figure out how this
> happened. So thats why my question, how can I avoid multiple inclusion
> of files.
>
> suresh


if you are writing the files that are included you can self-protect them

unlss defined? $mylib

module MyLib
end

end


however, if you expect something like rails or ramaze to autoload
these files in development mode think twice.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama