2010-07-29 5 views
1

폴더에 임의 모듈 파일이 있습니다.모듈 파일을 요구하고 해당 모듈과 상호 작용합니까?

예 : "module User"가 들어있는 "user.rb", "module Customer"가 들어있는 "customer.rb"등이 있습니다.

모든 파일을 요구하고 모든 모듈 방법을 인쇄하고 싶습니다. 여기

내 현재 코드입니다 :

@@data_module_methods = [] 

    # iterate through all files in the data folder 
    Dir[File.join(APP_ROOT, "data", "*.rb")].each do |file| 
    require file 
    # give me the module name from the filepath (so ./data/user.rb will give me User) 
    data_module_name = file.split("/").[](-1).split(".").[](0).capitalize 

    # ERROR: print all method names, HERE IT FAILS BECAUSE data_module_name is a string and not the module:) 
    data_module_name.instance_methods.each do |method| 
     @@data_module_methods << method 
    end 
    end 

내가 어떻게 이런 일을 할 수 있습니까?

감사

답변

3

당신은 정말 그 이름에 의해 모든 모듈을 얻기 위해 Kernel#const_get 방법을 사용할 수 있습니다 :

... 
Kernel.const_get(data_module_name).instance_methods.each do |method| 
...
관련 문제