2013-11-02 3 views
0
module YourApp 
    class Application < Rails::Application 
     config.my_custom_variable = :custom_value 
    end 
end 

내 레일 애플리케이션에서 작동합니다. 나는 루비 관점에서 이것이 어떻게 작동하는지 이해하고 싶다. 내 최소한의 루비 지식에 따라 config (Rails :: Application :: Configuration) 객체의 my_custom_variable에 대한 getter 및 setter (my_custom_variable =)가 있어야합니다. 이것이 내 사용자 지정 변수이기 때문에 Configuration 개체 인스턴스에는 나타나지 않습니다. 어떻게 동적으로 생성/추가됩니다. ?Ruby에서 변수 설정 및 액세스

누군가 설명해 주실 수 있습니까?, 이것을 이해하기 위해 적절한 문서를 보내주십시오.

답변

2

레일은 을 사용하여 config에서 호출되는 모든 방법을 포착합니다. 그런 다음 옵션의 해시에 추가합니다.

관련 소스 코드 here을 볼 수 있습니다.

0

레일은 그것을 구현하지만

require 'ostruct' 

module YourApp 
    class Application 
     @@config = OpenStruct.new 

     def self.config 
      return @@config 
     end 
    end 
end 

YourApp::Application.config.my_custom_variable = :custom_value 
puts YourApp::Application.config.my_custom_variable 
>> custom_value 
루비

에 비슷한 기능을 달성 할 방법