class TestController < ApplicationController
def test
@goodbay = TestClass.varible
end
end
class TestClass
@@varible = "var"
end
と私が間違っているどのようなエラークラス変数にアクセスするにはどうすればよいですか?
undefined method 'varible' for TestClass:Class
ライン上
@goodbay = TestClass.varible
を取得しますか?
あなたは 'cattr_accessor'を意味しませんでしたか? :) –
cattr_accessorはより良い名前ですが、それはmattr_accessorのエイリアスです(リンクごとに)。 – Phrogz
また、 'Object#instance_variable_get'はすべてのインスタンス変数(' @ ')へのアクセスを許可し、' Object#class_variable_get'はクラス変数( '@@')に対して同じものを提供するので、アクセッサメソッドを持つ必要はありません。明示的なゲッターメソッドの必要性'TestClass.instance_variable_get(:@ variable)'または 'TestClass.class_variable_get(:@@ variable)' – engineersmnky