2014-11-13 1 views
1

최근 집에서 구성한 구성 모듈을 Configatron으로 대체했지만 최근 유스 케이스를 사용할 수 없습니다.Object.const_get에 대한 매개 변수로 값을 사용할 때 Configatron :: Store를 String으로 변환 할 수 없습니다.

나는이 같은 Object.const_get하는 인수로 configatron 값을 사용하려고 할 때 :

def formatter_class 
    Object.const_get(configatron.formatter) 
end 

나는 다음과 같은 오류를 얻을 : (

file.rb:10:in `const_get': can't convert Configatron::Store to String 
    (Configatron::Store#to_str gives Configatron::Store) (TypeError) 

configatron 할당은 다음과 같습니다 단순화) :

configatron.formatter = case 
          when condition? 
          'ExportFormat' 
          else 
          'ScreenFormat' 
         end 

내가 configatron.formatter = 'ScreenFormat' 일지라도 오류.

formatter_class 방법에 대한 변형도 시도해 보았습니다. 물론

def formatter_class 
    Object.const_get(configatron['formatter']) 
end 

,이 작업이 성공,하지만 내 사용 사례 이행하지 않습니다 :이 실패

def formatter_class 
    Object.const_get('ScreenFormat') 
end 

내가 잘못하고있는 중이 야 무엇을?

답변

2

내 문제가 해결되었습니다. 초기화되지 않은 경우 configatron.whatever에 전화 할 수 있으며 Configatron::Store을 반환합니다.

값에 액세스하기 전에 configatron.has_key? 'formatter'에 대한 호출을 삽입했습니다. false를 리턴했을 때, 값이 아직 초기화되지 않은 코드 경로에서 오류가 발생했다는 것을 알아 냈습니다. 일단 값을 초기화하면 더 이상 오류가 발생하지 않습니다.