あるファイルからはモジュールの関数にアクセスできるが、別のファイルからはアクセスできないという状況があります。これらのファイルは両方とも同じディレクトリにあります。できる限りコードを再作成してみます。
ディレクトリ構造:
init.rb
lib/FileGenerator.rb
lib/AutoConfig.rb
lib/modules/helpers.rb
lib/AutoConfig.rb
#!/usr/bin/env ruby
require 'filegenerator'
require 'modules/helpers'
class AutoConfig
include Helpers
def initialize
end
def myFunction
myhelper #here's the module function
end
def mySecondFunction
FileGenerator.generatorFunction # call to the FileGenerator
end
end
lib/FileGenerator.rb
#!/usr/bin/env ruby
require 'modules/helpers'
class FileGenerator
include Helpers
def initialize
end
def self.generatorFunction
myhelper #here's the module function that doesn't work
end
end
lib/modules/helper.rb
#!/usr/bin/env ruby
module Helpers
def myhelper
#Do Stuff
end
end
AutoConfig ファイルは、アプリの主力です。モジュール関数を呼び出すと、myhelper
何の問題もありません。AutoConfig は途中で を呼び出しますFileGenerator.generatorFunction
。
にFileGenerator.generatorFunction
も同じモジュール関数が含まれていますが、何らかの理由でプログラムを実行すると次のエラーが発生します。
filegenerator.rb:26:in `generatorFunction': undefined method `myhelper' for FileGenerator:Class (NoMethodError)
私はこれで数時間、さまざまな組み合わせを試してみましたが、どこが間違っているのかわかりません。どんな助けでも大歓迎です。