[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Loading classes in order

Elias Orozco

4/3/2009 8:53:00 PM

Hello guys,

I have a folder that contains several classes. There are some some
dependencies between them as one class could extend from another one
included in the folder. I am trying to require them all dynamically but
I get:

uninitialized constant <name of the class>

This happens because I try to require a class that extends from a class
I haven't require yet. So can anyone help or give me any hint on how to
load them all dynamically and deal with those dependencies?

Thanks,

Elías
--
Posted via http://www.ruby-....

16 Answers

Joel VanderWerf

4/3/2009 9:09:00 PM

0

Elias Orozco wrote:
> Hello guys,
>
> I have a folder that contains several classes. There are some some
> dependencies between them as one class could extend from another one
> included in the folder. I am trying to require them all dynamically but
> I get:
>
> uninitialized constant <name of the class>
>
> This happens because I try to require a class that extends from a class
> I haven't require yet. So can anyone help or give me any hint on how to
> load them all dynamically and deal with those dependencies?
>
> Thanks,
>
> Elías

Can you insert requires in these files to make the dependencies explicit?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Elias Orozco

4/3/2009 9:19:00 PM

0

Hi Joel thanks for the reply. The idea is to load them at once. Without
specifying requires inside each class. I have seen that when I put a
series of classes on the rails lib folder everything loads without
problem. I have looked into rails itself, but I don't get how it does
that.

Thanks,

Elías

Joel VanderWerf wrote:
> Elias Orozco wrote:
>> I haven't require yet. So can anyone help or give me any hint on how to
>> load them all dynamically and deal with those dependencies?
>>
>> Thanks,
>>
>> Elías
>
> Can you insert requires in these files to make the dependencies
> explicit?

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

Iñaki Baz Castillo

4/3/2009 9:24:00 PM

0

El Viernes 03 Abril 2009, Elias Orozco escribi=C3=B3:
> Hello guys,
>
> I have a folder that contains several classes. There are some some
> dependencies between them as one class could extend from another one
> included in the folder. I am trying to require them all dynamically but
> I get:
>
> uninitialized constant <name of the class>
>
> This happens because I try to require a class that extends from a class
> I haven't require yet. So can anyone help or give me any hint on how to
> load them all dynamically and deal with those dependencies?

path =3D File.dirname(__FILE__) + "/files_dir"

require "#{path}/required_file1"
require "#{path}/required_file2"
require "#{path}/required_file3"

Dir.chdir(path)
Dir["*.rb"].each do |file|
require "#{path}/#{file}"
end


=2D-=20
I=C3=B1aki Baz Castillo <ibc@aliax.net>

Elias Orozco

4/3/2009 9:33:00 PM

0

Iñaki Baz Castillo wrote:
> El Viernes 03 Abril 2009, Elias Orozco escribió:
>> I haven't require yet. So can anyone help or give me any hint on how to
>> load them all dynamically and deal with those dependencies?
>
> path = File.dirname(__FILE__) + "/files_dir"
>
> require "#{path}/required_file1"
> require "#{path}/required_file2"
> require "#{path}/required_file3"
>
> Dir.chdir(path)
> Dir["*.rb"].each do |file|
> require "#{path}/#{file}"
> end

Thanks Inaki, but in that example I will have to specify the require
files and that's exactly what I don't want. A lot of classes will be
copied into that folder and I can't hard-code all those requires.
--
Posted via http://www.ruby-....

Robert Klemme

4/3/2009 9:58:00 PM

0

On 03.04.2009 23:32, Elias Orozco wrote:
> Iñaki Baz Castillo wrote:
>> El Viernes 03 Abril 2009, Elias Orozco escribió:
>>> I haven't require yet. So can anyone help or give me any hint on how to
>>> load them all dynamically and deal with those dependencies?
>> path = File.dirname(__FILE__) + "/files_dir"
>>
>> require "#{path}/required_file1"
>> require "#{path}/required_file2"
>> require "#{path}/required_file3"
>>
>> Dir.chdir(path)
>> Dir["*.rb"].each do |file|
>> require "#{path}/#{file}"
>> end
>
> Thanks Inaki, but in that example I will have to specify the require
> files and that's exactly what I don't want. A lot of classes will be
> copied into that folder and I can't hard-code all those requires.

Personally I would prefer to make dependencies explicit and insert
require statements in all of those files. That's the most reasonable
solution and then the approach from above will work properly.

An alternative might be to rescue any exceptions and require files again.

Cheers

robert

Iñaki Baz Castillo

4/3/2009 10:00:00 PM

0

El Viernes 03 Abril 2009, Elias Orozco escribi=C3=B3:
> I=C3=B1aki Baz Castillo wrote:
> > El Viernes 03 Abril 2009, Elias Orozco escribi=C3=B3:
> >> I haven't require yet. So can anyone help or give me any hint on how to
> >> load them all dynamically and deal with those dependencies?
> >
> > path =3D File.dirname(__FILE__) + "/files_dir"
> >
> > require "#{path}/required_file1"
> > require "#{path}/required_file2"
> > require "#{path}/required_file3"
> >
> > Dir.chdir(path)
> > Dir["*.rb"].each do |file|
> > require "#{path}/#{file}"
> > end
>
> Thanks Inaki, but in that example I will have to specify the require
> files and that's exactly what I don't want. A lot of classes will be
> copied into that folder and I can't hard-code all those requires.

When failing due to a non existing class, Ruby raises a NameError exception.
Then you can add something as:

=2D--------------------------
failed_files=3D[]

path =3D xxxxxx
Dir.chdir(path)

Dir["*.rb"].each do |file|
begin
require "#{path}/#{file}"
rescue NameError
failed_files << file
end
end

failed_files.each do |file|
require "#{path}/#{file}"
end
=2D---------------------------

(It should be very improved however).

=2D-=20
I=C3=B1aki Baz Castillo <ibc@aliax.net>

Elias Orozco

4/3/2009 10:31:00 PM

0

Hey Inaki thanks,

I think I'll have to go that way. I wonder if that is how rails does to
load classes when the server is initialize. I wandered around the
initializer file but couldn't get how it was done. Thanks again,

Elias

Iñaki Baz Castillo wrote:
> El Viernes 03 Abril 2009, Elias Orozco escribió:
>> >
>> > Dir.chdir(path)
>> > Dir["*.rb"].each do |file|
>> > require "#{path}/#{file}"
>> > end
>>
>> Thanks Inaki, but in that example I will have to specify the require
>> files and that's exactly what I don't want. A lot of classes will be
>> copied into that folder and I can't hard-code all those requires.
>
> When failing due to a non existing class, Ruby raises a NameError
> exception.
> Then you can add something as:
>
> ---------------------------
> failed_files=[]
>
> path = xxxxxx
> Dir.chdir(path)
>
> Dir["*.rb"].each do |file|
> begin
> require "#{path}/#{file}"
> rescue NameError
> failed_files << file
> end
> end
>
> failed_files.each do |file|
> require "#{path}/#{file}"
> end
> ----------------------------
>
> (It should be very improved however).

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

David Masover

4/3/2009 11:33:00 PM

0

Hey, top-posting is annoying. You're replying above what everyone else wrote,
which means people have no context.

On Friday 03 April 2009 16:18:55 Elias Orozco wrote:
> Hi Joel thanks for the reply. The idea is to load them at once. Without
> specifying requires inside each class.

Requires don't need to be inside classes, just files. You can have more than
one class per file, or a file that takes up multiple classes.

> I have seen that when I put a
> series of classes on the rails lib folder everything loads without
> problem. I have looked into rails itself, but I don't get how it does
> that.

Rails intercepts Object#cost_missing, and uses that to load anything with the
same name. Basically, this means that the first time you try to use a class, it
gets loaded.

I wrote a little library that does something similar, but with Kernel#autoload
instead -- it's slightly faster and more flexible:

http://github.com/masover/...

You can install that, and it will either use activesupport (from Rails), or
extlib (used by Merb), depending what's available (or what you already
loaded).

If it doesn't work for you, it should be small enough to be readable.

Robert Klemme

4/4/2009 12:06:00 PM

0

On 04.04.2009 00:31, Elias Orozco wrote:
> I think I'll have to go that way.

I do not understand why you seem to be so reluctant to declare
dependencies properly via "require". Can you explain that? The time it
takes to discuss this and search Rails source code is almost certainly
more than what it takes to simply use "require" the way it was intended to.

> I wonder if that is how rails does to
> load classes when the server is initialize. I wandered around the
> initializer file but couldn't get how it was done. Thanks again,

Are you sure you can throw a number of files with dependencies into a
folder and Rails will somehow figure the load order?

Regards

robert


PS: Please do not top post.

Elias Orozco

4/4/2009 6:24:00 PM

0

Robert Klemme wrote:
> On 04.04.2009 00:31, Elias Orozco wrote:
>> I think I'll have to go that way.
>
> I do not understand why you seem to be so reluctant to declare
> dependencies properly via "require". Can you explain that? The time it
> takes to discuss this and search Rails source code is almost certainly
> more than what it takes to simply use "require" the way it was intended
> to.
>
> > I wonder if that is how rails does to
>> load classes when the server is initialize. I wandered around the
>> initializer file but couldn't get how it was done. Thanks again,
>
> Are you sure you can throw a number of files with dependencies into a
> folder and Rails will somehow figure the load order?
>
> Regards
>
> robert
>
>
> PS: Please do not top post.

The app I'm working on is an online editor where a user create classes
that connect to back-ends and fetch data. The users can create classes
and have them all in their personal folder (security issues with this
type of apps are high, I'm working on that too). So basically when the
user wants to initialize an object of a class that depends on another
one I don't want uninitialized constant errors. I could make require as
it is the way is intended to do as you say. But I have seen that when I
put all those classes in the rails lib folder, for example, they're all
loaded without problem when I initialize the server and I can make an
instance object of any class without any error. I want something like
that.

Thanks,

Elias

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