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?
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
Dir.chdir(path)
Dir["*.rb"].each do |file|
require "#{path}/#{file}"
end
···
El Viernes 03 Abril 2009, Elias Orozco escribió:
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?
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?
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-forum.com/\.
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
···
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?
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:
···
El Viernes 03 Abril 2009, Elias Orozco escribió:
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.
---------------------------
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
----------------------------
Hey, top-posting is annoying. You're replying above what everyone else wrote,
which means people have no context.
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:
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.
···
On Friday 03 April 2009 16:18:55 Elias Orozco wrote:
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
----------------------------
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?
Look at Merb. Don't go the horrible ActiveSupport route.
Merb's loader works somewhat similarly to what Inaki pasted, except it
loops, retrying the failed files. If it loops through the failed files and
no new ones are loaded it gives up.
···
On Fri, Apr 3, 2009 at 4:31 PM, Elias Orozco <elioncho@gmail.com> wrote:
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,
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.
Imagining a very complex case it could occur that class_A defined in file_A
depends on class_C defined in file_C, and class_C depends on class_B defined
in file_B.
Loading in alphabetic order (A, B, C) would load just file_B. So file_A and
file_C would require a new try. Trying again in alphabetic order (A, C) would
load just file_C, and file_A would fail.
So a third attemp would load just file_A (succesfully now).
I wonder if could be a case in which this approach wouldn't work.
···
El Sábado 04 Abril 2009, Tony Arcieri escribió:
Merb's loader works somewhat similarly to what Inaki pasted, except it
loops, retrying the failed files. If it loops through the failed files and
no new ones are loaded it gives up.
Another approach would be loading first a file containing all the classes
"declaration", this is:
···
El Sábado 04 Abril 2009, Elias Orozco escribió:
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.
----------
class AAA ; end
class BBB ; end
[...]
----------
and later load all the files with classes real "definition".
Not sure if this option is suitable in your case however.
On Sat, Apr 4, 2009 at 12:04 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
El Sábado 04 Abril 2009, Tony Arcieri escribió:
> Merb's loader works somewhat similarly to what Inaki pasted, except it
> loops, retrying the failed files. If it loops through the failed files
and
> no new ones are loaded it gives up.
Imagining a very complex case it could occur that class_A defined in file_A
depends on class_C defined in file_C, and class_C depends on class_B
defined
in file_B.
Loading in alphabetic order (A, B, C) would load just file_B. So file_A and
file_C would require a new try. Trying again in alphabetic order (A, C)
would
load just file_C, and file_A would fail.
So a third attemp would load just file_A (succesfully now).
I wonder if could be a case in which this approach wouldn't work.
Imagining a very complex case it could occur that class_A defined in file_A
depends on class_C defined in file_C, and class_C depends on class_B
defined in file_B.
Ruby can't require the classes so strictly. C++ could, and maybe Java could. But a Duck Typing language simply cannot require that! A given implementation of that design might have some mistake that forces the require order, but the mistake itself is always fixable.
For example, don't include this above your first require 'test/unit':
module Test::Unit::Assertions
def test_my_awesome_assertion
end
end
The fix is very simple:
module Test; module Unit; module Assertions
def test_my_awesome_assertion
end
end; end; end
Now that can harmlessly appear above the original module Assertions.