From: Brian Candler Date: 2009-02-17T05:27:14+09:00 Subject: Re: load a Ruby source with params Sai Hl wrote: > I am going to try to use the (2) solutions, with a module for any files > like: > > # inc.test.rb > module Inc > def say > 'Hello' + subject > end > end > > But is this possible in my case, because I don't know the name of each > module (which is depend of the name of its file)? If you are consistent in your naming, you can map the filename to the module name (or vice versa). For example, look at the underscore and camelize methods of ActiveSupport from Rails. These just convert FooBar" to "foo_bar", and vice versa. Then you can use const_get to convert the name "FooBar" to the actual module instance on which to invoke the method. e.g. puts Object.const_get("Inc").say(subject) A simpler solution is just to iterate over all the modules within an enclosing module/namespace: ==> inc1.rb <== module Extensions module Inc1 def self.say(subject) "Hello #{subject}" end end end ==> inc2.rb <== module Extensions module Inc2 def self.say(subject) "Goodbye #{subject}" end end end ==> main.rb <== Dir["inc*.rb"].each { |src| load src } Extensions.constants.each do |k| puts Extensions.const_get(k).say("world") end Aside: if you strip off the .rb extension (look at File.basename) then you can use 'require' instead of 'load'. This prevents loading the same file multiple times. -- Posted via http://www.ruby-forum.com/.