Executing one of several ruby objects

Discussion in 'Ruby' started by dhf0820@gmail.com, Nov 5, 2009.

  1. Guest

    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
     
    , Nov 5, 2009
    #1
    1. Advertising

  2. 2009/11/5 <>:
    > 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/
     
    Robert Klemme, Nov 5, 2009
    #2
    1. Advertising

  3. Don French Guest

    On Nov 4, 10:13 pm, Robert Klemme <> wrote:
    > 2009/11/5 <>:
    >
    > > 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/



    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 French, Nov 5, 2009
    #3
  4. Don French wrote:
    > On Nov 4, 10:13�pm, Robert Klemme <> 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/.
     
    Aldric Giacomoni, Nov 5, 2009
    #4
  5. Don French Guest

    On Nov 5, 9:22 am, Aldric Giacomoni <> wrote:
    > Don French wrote:
    > > On Nov 4, 10:13 pm, Robert Klemme <> 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/.


    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, Nov 5, 2009
    #5
  6. Don French Guest

    On Nov 5, 9:22 am, Aldric Giacomoni <> wrote:
    > Don French wrote:
    > > On Nov 4, 10:13 pm, Robert Klemme <> 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/.


    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
     
    Don French, Nov 5, 2009
    #6
  7. 2009/11/5 Don French <>:
    > 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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Nov 6, 2009
    #7
  8. 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
     
    Joel VanderWerf, Nov 6, 2009
    #8
  9. On Friday 06 November 2009 04:08:45 am Robert Klemme wrote:
    > 2009/11/5 Don French <>:
    > > 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.
     
    David Masover, Nov 7, 2009
    #9
  10. 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 <>:
    >>> 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

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Nov 7, 2009
    #10
  11. James Britt Guest

    James Britt, Nov 7, 2009
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 7stud
    Replies:
    11
    Views:
    713
    Dennis Lee Bieber
    Mar 20, 2007
  2. John
    Replies:
    11
    Views:
    592
  3. Guest
    Replies:
    1
    Views:
    716
    Marc 'BlackJack' Rintsch
    Mar 27, 2008
  4. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    188
    Max Lapshin
    Mar 16, 2007
  5. Replies:
    4
    Views:
    82
Loading...

Share This Page