2014-09-09 2 views
0

나는 다음과 같은 루비 클래스가 :루비 클래스가 예기치 않게 작동하는 이유는 무엇입니까?

class Scheme 
    attr_reader :id, :uri, :labels, :in_schemes, :top_concepts 

    def initialize(id, uri, labels, in_schemes) 
    @id = id, 
    @uri = uri, 
    @labels = labels 
    @in_schemes = in_schemes 
    @top_concepts = Array.new 
    end 
end 

그리고이 (그들은 등, "01"와 같은 "01.01.00"를 이름이)가 포함 된 파일을 찾고 지정된 디렉토리를 통과하는 다음과 같은 방법 일련의 언어 별 카테고리 라벨 (아래 샘플) :

def make_schemes(catdir) 
    concept_schemes = Array.new 
    Dir.foreach(catdir) do |file| 
    unless file == "." || file == ".." 
     id = file.gsub(/\./,"").to_s 
     uri = "/unbist/scheme/#{id}" 
     labels = Array.new 
     in_schemes = Array.new 
     File.read("#{catdir}/#{file}").split(/\n/).each do |line| 
     label = JSON.parse(line) 
     labels << label 
     end 
     if id.size > 2 
     in_schemes = ["/unbist","/unbist/#{id[0..1]}"] 
     else 
     in_schemes = ["/unbist"] 
     end 
     p "Making new concept scheme with id: #{id}" 
     concept_scheme = Scheme.new(id, uri, labels, in_schemes) 
     p concept_scheme 
     concept_schemes << concept_scheme 
    end 
    end 
    return concept_schemes 
end 

"# {dir}/01"이라는 샘플 카테고리 파일. 각 라인은 적절한 JSON이지만,이 질문의 범위를 벗어나는 이유 때문에 전체 파일은 그렇지 않습니다.

{ "text": "ﻢﺳﺎﺌﻟ ﻕﺎﻧﻮﻨﻳﺓ ﻮﺴﻳﺎﺴﻳﺓ", "language": "ar" } 
{ "text": "政治和法律问题", "language": "zh" } 
{ "text": "political and legal questions", "language": "en" } 
{ "text": "questions politiques et juridiques", "language": "fr" } 
{ "text": "ПОЛИТИЧЕСКИЕ И ЮРИДИЧЕСКИЕ ВОПРОСЫ", "language": "ru" } 
{ "text": "cuestiones politicas y juridicas", "language": "es" } 

출력이 이상합니다. make_schemes 메서드의 id 변수는 새 Scheme을 만들기 전에 적절하게 설정되지만 Scheme 초기화 프로그램은 어딘가에서 혼란스럽게 보일뿐 아니라 전체 변수 집합을 객체의 id 변수에 적용합니다. ?.

"Making new concept scheme with id: 01" 
#<Scheme:0xa00ffc8 
@uri="/scheme/01", 
@labels=[{"text"=>"مسائل قانونية وسياسية", "language"=>"ar"}, {"text"=>"政治和法律问题", "language"=>"zh"}, {"text"=>"political and legal questions", "language"=>"en"}, {"text"=>"questions politiques et juridiques", "language"=>"fr"}, {"text"=>"ПОЛИТИЧЕСКИЕ И ЮРИДИЧЕСКИЕ ВОПРОСЫ", "language"=>"ru"}, {"text"=>"cuestiones politicas y juridicas", "language"=>"es"}], 
@id=["01", "/scheme/01", [{"text"=>"مسائل قانونية وسياسية", "language"=>"ar"}, {"text"=>"政治和法律问题", "language"=>"zh"}, {"text"=>"political and legal questions", "language"=>"en"}, {"text"=>"questions politiques et juridiques", "language"=>"fr"}, {"text"=>"ПОЛИТИЧЕСКИЕ И ЮРИДИЧЕСКИЕ ВОПРОСЫ", "language"=>"ru"}, {"text"=>"cuestiones politicas y juridicas", "language"=>"es"}]], 
@in_schemes=["/"], 
@top_concepts=[]> 

내가 여기 실종이 원인이 내가 비슷한 논리로 잘 작동 다른 클래스의 생성자가 I '다음은 위의 샘플에 대한 몇 가지 출력 (청소 개행 문자는 가독성을 위해 추가된다 . 당황 해요 어쩌면 더 잘 작동 것 접근법있다

답변

3

시도 고정 :

@uri = uri, 

에 :

@uri = uri 
있는 그대로, 루비 이야기하고

:

@uri = uri, @labels = labels  

어느, 나는 그것을 읽을 때, 당신은 다음 @uri에 해당 배열을 할당 uri, @labels의 배열에 labels을 할당하고 의미합니다.

+0

Waugh! 나는 그것을 볼 수 없다고 나는 믿을 수 없다. 나는 이틀 동안 그것을 꼼짝 않고 바라보고 있었다. 감사 :) –

관련 문제