Executing one of several ruby objects

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 F.

2009/11/5 [email protected] [email protected]:

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?

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

On Nov 4, 10:13 pm, Robert K. [email protected] wrote:


remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/

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

Don F. wrote:

On Nov 4, 10:13�pm, Robert K. [email protected] wrote:


remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/

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?

On Nov 5, 9:22 am, Aldric G. [email protected] wrote:

work. They are not know until run-time. It will include rb file from a
Don

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

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

Posted viahttp://www.ruby-forum.com/.

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

On Nov 5, 9:22 am, Aldric G. [email protected] wrote:

work. They are not know until run-time. It will include rb file from a
Don

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

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

Posted viahttp://www.ruby-forum.com/.

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

2009/11/5 Don F. [email protected]:

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

Don F. wrote:

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.

This may help…

http://redshift.sourceforge.net/script/

On Friday 06 November 2009 04:08:45 am Robert K. wrote:

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.

[email protected] wrote:

Any pointers on this?

There may be some ideas to steal from this:


James B.

www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff
www.neurogami.com - Smart application development

On 07.11.2009 05:21, David M. wrote:

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:

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