Executing one of several ruby objects

D

dhf0820

I am trying to write a program that will load a series of DSLs (ruby
files) into it and then call the same class method in each one until
one returns something other than nil. The current DSL is just a ruby
class that subclasses the same "descriptor" superclass. The number of
DSLs loaded is not know until runtime as it is all the files in a
directory.


Any pointers on this?

Don French
 
R

Robert Klemme

2009/11/5 [email protected] said:
I am trying to write a program that will load a series of DSLs (ruby
files) into it and then call the same class method in each one until
one returns something other than nil. =A0The current DSL is just a ruby
class that subclasses the same "descriptor" superclass. The number of
DSLs loaded is not know until runtime as it is all the files in a
directory.


Any pointers on this?

Not sure what you're after but it seems a simple iteration will do:

res =3D nil
class =3D classes.find {|cl| res =3D cl.your_method}

If you do not need the class you can remove the second assignment.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
D

Don French

Not sure what you're after but it seems a simple iteration will do:

res = nil
class = classes.find {|cl| res = cl.your_method}

If you do not need the class you can remove the second assignment.

Kind regards

robert


Thank you for the reply.

The problem is normally you have to specify each of the modules or
external classes you are going to use in the source. That does not
work. They are not know until run-time. It will include rb file from a
specified director. Each of the files in the directory will have a
"descriptor" in it that has the "identification" class method that
will be called. When it identifies what is should it will return a non
nil object.

I understanding the calling of the method, it is how to load each of
those files into an array so that they can be executed when needed. I
am probably making this harder than it should be.

Mahalo (thank you)
Don
 
A

Aldric Giacomoni

Don said:
Thank you for the reply.

The problem is normally you have to specify each of the modules or
external classes you are going to use in the source. That does not
work. They are not know until run-time. It will include rb file from a
specified director. Each of the files in the directory will have a
"descriptor" in it that has the "identification" class method that
will be called. When it identifies what is should it will return a non
nil object.

I understanding the calling of the method, it is how to load each of
those files into an array so that they can be executed when needed. I
am probably making this harder than it should be.

Mahalo (thank you)
Don

a = Dir.glob '*'
a.delete_if { |node| File.directory? node }

Woo! List of files in the current directory!
.. Is that what you want?
 
D

Don French

Thank you for the reply.
The problem is normally you have to specify each of the modules or
external classes you are going to use in the source. That does not
work. They are not know until run-time. It will include rb file from a
specified director. Each of the files in the directory will have a
"descriptor" in it that has the "identification" class method that
will be called. When it identifies what is should it will return a non
nil object.
I understanding the calling of the method, it is how to load each of
those files into an array so that they can be executed when needed.  I
am probably making this harder than it should be.
Mahalo (thank you)
Don

a = Dir.glob '*'
a.delete_if { |node| File.directory? node }

Woo! List of files in the current directory!
. Is that what you want?

Ok, that gives me the list of the files. how do I execute a known
class method call it self.identify in each of them, these are all of
the same class. That is still the part I do not understand.

Thank you
Don
 
D

Don French

Thank you for the reply.
The problem is normally you have to specify each of the modules or
external classes you are going to use in the source. That does not
work. They are not know until run-time. It will include rb file from a
specified director. Each of the files in the directory will have a
"descriptor" in it that has the "identification" class method that
will be called. When it identifies what is should it will return a non
nil object.
I understanding the calling of the method, it is how to load each of
those files into an array so that they can be executed when needed.  I
am probably making this harder than it should be.
Mahalo (thank you)
Don

a = Dir.glob '*'
a.delete_if { |node| File.directory? node }

Woo! List of files in the current directory!
. Is that what you want?

I had said I understood calling the method. that is the part that I do
not understand. how to call a method in each of the files I now have
in the array.

thanks
Don
 
R

Robert Klemme

2009/11/5 Don French said:
Ok, that gives me the list of the files. how do I execute a known
class method call it self.identify in each of them, these are all of
the same class. That is still the part I do not understand.

I typically find it easier to use a registration process, e.g.

in mod1.rb:

class X
def self.identify; "foo"; end
end

::MODULES << X

in mod2.rb:

class Y
def self.idenfity; "bar"; end
end

::MODULES << Y

main.rb:

require 'set'
MODULES = Set.new

Dir["*.rb"].each {|f| load(f)}

MODULES.each do |mod|
mod.identify
end

Cheers

robert
 
D

David Masover

I typically find it easier to use a registration process, e.g.

in mod1.rb:

class X
def self.identify; "foo"; end
end

::MODULES << X

I usually like to do that implicitly. For example, all these files probably
have something in common, or they wouldn't be called this way. Make them
either inherit from a common ancestor or include a common module. Here's how
to do it with a module:

require 'set'
module Foo
Children = Set.new
def self.included klass
Children << klass
end
end

This is just as easy to do with classes -- just use inherited instead of
included.

And then, to borrow an earlier example:

result = nil
klass = Foo::Children.find {|klass|
result = klass.send :some_method
}


Another possibility would be to enforce a strict naming convention -- for
example, if there's a foo.rb, you assume it contains a Foo class. You could do
something like:

require 'extlib'
classes = filenames.map{|n| Object.const_get n.camel_case}

...depending on what library you use to get the camel_case method. Or you
could do it yourself with a simple regex.
 
R

Robert Klemme

I usually like to do that implicitly. For example, all these files probably
have something in common, or they wouldn't be called this way. Make them
either inherit from a common ancestor or include a common module. Here's how
to do it with a module:

Of course, the process can be improved. Thanks for the suggestions. My
main point was to not reach from the outside into the file but rather
reverse the process, i.e. code in the file announces its availability.

One just needs to make sure that deeper inheritance is handled in the
way as intended.
Another possibility would be to enforce a strict naming convention -- for
example, if there's a foo.rb, you assume it contains a Foo class. You could do
something like:

Something I'd rather not do because finding things about naming
conventions and const_get is probably among the most unobvious and
difficult to understand approaches.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top