# Executing one of several ruby objects

**URL:** https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095
**Category:** ruby-talk
**Created:** [5 November 2009 07:35 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095 "2009-11-05T07:35:12Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Don\_French](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@Don\_French](https://rubytalk.org/u/Don_French)
#### Post date: [5 November 2009 07:35 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/1 "2009-11-05T07:35:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [5 November 2009 08:13 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/2 "2009-11-05T08:13:27Z")

</div>

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

> **···**
>
> 2009/11/5 dhf0820@gmail.com \<dhf0820@gmail.com\>:
> 
> > 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?
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

### Author: ![James\_Britt3](https://avatars.discourse-cdn.com/v4/letter/j/b782af/32.png) [@James\_Britt3](https://rubytalk.org/u/James_Britt3)
#### Post date: [7 November 2009 19:24 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/3 "2009-11-07T19:24:53Z")

</div>

dhf0820@gmail.com wrote:

> Any pointers on this?

There may be some ideas to steal from this:

> **[Neurogami - MOP-ing Up with Herbal Cialis](https://www.neurogami.com/articles/Mop-ing_up_with_herbal_cialis/)**
>
> Neurogami: Avant-garage research + development

> **···**
>
> --  
> James Britt
> 
> [www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
> [www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
> [www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [www.neurogami.com](http://www.neurogami.com) - Smart application development

---

<div class="post-metadata">

### Author: ![Don\_French](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@Don\_French](https://rubytalk.org/u/Don_French)
#### Post date: [5 November 2009 19:15 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/4 "2009-11-05T19:15:14Z")

</div>

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

> **···**
>
> On Nov 4, 10:13 pm, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> > 2009/11/5 dhf0...@gmail.com \<dhf0...@gmail.com\>:
> > 
> > \> 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
> > 
> > --  
> > remember.guy do |as, often| as.you\_can - without endhttp://blog.rubybestpractices.com/

---

<div class="post-metadata">

### Author: ![Aldric\_Giacomoni2](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@Aldric\_Giacomoni2](https://rubytalk.org/u/Aldric_Giacomoni2)
#### Post date: [5 November 2009 19:22 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/5 "2009-11-05T19:22:52Z")

</div>

Don French wrote:

> **···**
>
> > On Nov 4, 10:13�pm, Robert Klemme \<shortcut...@googlemail.com\> 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?  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Don\_French](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@Don\_French](https://rubytalk.org/u/Don_French)
#### Post date: [5 November 2009 21:45 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/6 "2009-11-05T21:45:05Z")

</div>

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 Giacomoni \<ald...@trevoke.net\> wrote:
> 
> > Don French wrote:  
> > \> On Nov 4, 10:13 pm, Robert Klemme \<shortcut...@googlemail.com\> 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?  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

### Author: ![Don\_French](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@Don\_French](https://rubytalk.org/u/Don_French)
#### Post date: [5 November 2009 21:45 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/7 "2009-11-05T21:45:05Z")

</div>

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

> **···**
>
> On Nov 5, 9:22 am, Aldric Giacomoni \<ald...@trevoke.net\> wrote:
> 
> > Don French wrote:  
> > \> On Nov 4, 10:13 pm, Robert Klemme \<shortcut...@googlemail.com\> 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?  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [6 November 2009 10:08 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/8 "2009-11-06T10:08:45Z")

</div>

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

in mod1.rb:

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

::MODULES \<\< X

in mod2.rb:

class Y  
&nbsp;&nbsp;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|  
&nbsp;&nbsp;mod.identify  
end

Cheers

robert

> **···**
>
> 2009/11/5 Don French \<dhf0820@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.
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

### Author: ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)
#### Post date: [6 November 2009 17:43 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/9 "2009-11-06T17:43:14Z")

</div>

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/](http://redshift.sourceforge.net/script/)

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

### Author: ![David\_Masover](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@David\_Masover](https://rubytalk.org/u/David_Masover)
#### Post date: [7 November 2009 04:21 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/10 "2009-11-07T04:21:52Z")

</div>

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  
&nbsp;&nbsp;Children = Set.new  
&nbsp;&nbsp;def self.included klass  
&nbsp;&nbsp;&nbsp;&nbsp;Children \<\< klass  
&nbsp;&nbsp;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|  
&nbsp;&nbsp;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.

> **···**
>
> On Friday 06 November 2009 04:08:45 am Robert Klemme wrote:
> 
> > 2009/11/5 Don French \<dhf0820@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  
> > &nbsp;&nbsp;def self.identify; "foo"; end  
> > end
> > 
> > ::MODULES \<\< X

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [7 November 2009 09:25 UTC](https://rubytalk.org/t/executing-one-of-several-ruby-objects/56095/11 "2009-11-07T09:25:05Z")

</div>

> > > 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  
> > &nbsp;&nbsp;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

&nbsp;&nbsp;robert

> **···**
>
> 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 \<dhf0820@gmail.com\>:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
