Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Executing one of several ruby objects

21 views
Skip to first unread message

dhf...@gmail.com

unread,
Nov 5, 2009, 2:34:46 AM11/5/09
to
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

Robert Klemme

unread,
Nov 5, 2009, 3:13:27 AM11/5/09
to
2009/11/5 dhf...@gmail.com <dhf...@gmail.com>:

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

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

Don French

unread,
Nov 5, 2009, 2:11:36 PM11/5/09
to
On Nov 4, 10:13 pm, Robert Klemme <shortcut...@googlemail.com> wrote:
> 2009/11/5 dhf0...@gmail.com <dhf0...@gmail.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

Aldric Giacomoni

unread,
Nov 5, 2009, 2:22:52 PM11/5/09
to

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

Woo! List of files in the current directory!
. Is that what you want?
--
Posted via http://www.ruby-forum.com/.

Don French

unread,
Nov 5, 2009, 4:43:16 PM11/5/09
to

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

Don French

unread,
Nov 5, 2009, 4:44:57 PM11/5/09
to
On Nov 5, 9:22 am, Aldric Giacomoni <ald...@trevoke.net> 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.

thanks
Don

Robert Klemme

unread,
Nov 6, 2009, 5:08:45 AM11/6/09
to
2009/11/5 Don French <dhf...@gmail.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.

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

Joel VanderWerf

unread,
Nov 6, 2009, 12:43:14 PM11/6/09
to
Don French 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/

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

David Masover

unread,
Nov 6, 2009, 11:21:52 PM11/6/09
to
On Friday 06 November 2009 04:08:45 am Robert Klemme wrote:
> 2009/11/5 Don French <dhf...@gmail.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.
>
> 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.

Robert Klemme

unread,
Nov 7, 2009, 4:22:47 AM11/7/09
to
On 07.11.2009 05:21, David Masover wrote:
> On Friday 06 November 2009 04:08:45 am Robert Klemme wrote:
>> 2009/11/5 Don French <dhf...@gmail.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.
>> 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:

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

James Britt

unread,
Nov 7, 2009, 2:24:53 PM11/7/09
to
dhf...@gmail.com wrote:

>
>
> Any pointers on this?

There may be some ideas to steal from this:

http://www.neurogami.com/articles/Mop-ing_up_with_herbal_cialis/


--
James Britt

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

0 new messages