Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 是否需要模块文件并与其模块交互?_Ruby - Fatal编程技术网

Ruby 是否需要模块文件并与其模块交互?

Ruby 是否需要模块文件并与其模块交互?,ruby,Ruby,在一个文件夹中,我有随机的模块文件 例如,“user.rb”包含“module user”,“customer.rb”包含“module customer”等等 我需要所有文件并打印出所有模块方法 这是我目前的代码: @@data_module_methods = [] # iterate through all files in the data folder Dir[File.join(APP_ROOT, "data", "*.rb")].each do |file|

在一个文件夹中,我有随机的模块文件

例如,“user.rb”包含“module user”,“customer.rb”包含“module customer”等等

我需要所有文件并打印出所有模块方法

这是我目前的代码:

  @@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
@@data\u模块\u方法=[]
#遍历数据文件夹中的所有文件
Dir[File.join(APP_ROOT,“data”和“*.rb”)。每个do |文件|
需要文件
#从文件路径中给我模块名(因此./data/user.rb将给我user)
数据\u模块\u名称=文件.split(“/”)(-1).split(“.”)[(0).大写
#错误:打印所有方法名称,此处失败,因为数据\模块\名称是字符串,而不是模块:)
数据_模块_名称.实例_方法.每个do方法|

@@data_module_methods您可以使用
Kernel#const_get
方法按名称获取每个模块,因此:

...
Kernel.const_get(data_module_name).instance_methods.each do |method|
...