在模块中调用方法

时间:2016-05-02 12:24:38

标签: ruby

我有两个文件。文件X是:

module A
    module B    
        XYZ = "5"    
        def testx
            puts "Test"
        end
    end
end

档案Y如下:

require_relative "./B/X"

def test
 puts A::B::XYZ
 puts A::B.testx
end

我正在testx内调用文件X的{​​{1}},但它无效。但是,对常量Y的调用是成功的。

我做错了什么?

更新:文件XYZ需要@AlexN指出的类结构,并且包含模块Y,然后可以简单地调用B。文件testx如下:

Y

2 个答案:

答案 0 :(得分:4)

ListBox1.Items.Clear();

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "AustraliaFolder")).Length != 0) 
{ 
     ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "AustraliaFolder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "DE1Folder")).Length != 0) 
{
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "DE1Folder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "PL1Folder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "PL1Folder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "EuropeFolder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "EuropeFolder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "fr1Folder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "fr1Folder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "FranceFolder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "FranceFolder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "HKFolder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "HKFolder"))[0]); 
}

if (Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "UKFolder")).Length != 0) 
{ 
    ListBox1.Items.Add(Directory.GetFiles(SalesInputFilePath + ResourceDetails.InputFolders(FormDMM, EH, ObjApp, Modules, "UKFolder"))[0]); 
}

该方法必须是类方法。请参阅此示例https://stackoverflow.com/a/5417596/1166593

答案 1 :(得分:0)

你这里不需要上课。以下是仅使用模块的方法:

module A
  module B    
    extend self
    XYZ = "5"    
    def testx
        puts "Test"
    end
  end
end

执行A::B.testx现在输出测试。该问题与模块的嵌套无关。如果您直接在testx下定义A,则问题会相同。