0

モジュール内からモジュール外で定義された変数を取得しようとしています

@variable = "variable"

module A
  def write
    puts @variable
  end
end

extend A
write # -> "variable"
# works ok, but i'll be loading a few modules with the same method name "write"
# need to be able to call A.write, A1.write, A2.write and so on

module B
  module_function
  def write
    puts @variable
  end
end

extend B
B.write # ->  
# Can call the B.write
# is returning nothing, not getting @variable

module C
  module_function
  def write var
    puts var
  end
end

extend C
C.write @variable # -> "variable"
# works ok

私が実際に良いと思った解決策はありますか?またはそれを行うより良い方法はありますか?定義を読んだ後もまだ取得できないため、module_function の呼び出しについてはわかりません。他に何をしているのかわかりません。

4

0 に答える 0