진행률 표시 줄과 함께 Ruby에서 CSV 가져 오기 도구를 만들려고합니다. 문제는 입니다. SiteController ProgressBar의 변수가 초기화되지 않습니다. 어떻게 제대로 할 수 있습니까? 컨트롤러의 변수에 액세스 할 수 있어야합니다. 하지만 include를 사용하여 작동하지 않는 이유는 무엇입니까?파일 사이의 Ruby 공유 변수
class SpecificImporter < Importer
include ProgressBar
def import
....
custom logic
Thread.new do
@rows.each do |r|
increment_bar
end
end
end
class Importer
attr_accessor :file
include ProgressBar
def calculate_max_rows
l = File.open(@file.path).count
set_max_rows(l)
end
end
module ProgressBar
attr_reader :cur,:max
def increment_bar
@cur += 1
end
def set_max_rows(val)
@max = val
end
def progress
@cur/@max
end
end
class SiteController < ApplicationController
include ProgressBar
def update_progress
..
#send data using json every x seconds
..
status = progress
end
end
주 부모 클래스가 이미 그것을했기 때문에'SpecificImporter'가'ProgressBar를 포함 할 필요가 없습니다. – tadman
또한,'set_max_rows '와 같은 것은 Ruby의 정신에 대항하는 것입니다. 해당 속성에 쓸 수 있도록하려면'attr_accessor'를 사용하면'max ='메소드를 얻을 수 있습니다. 명확하게하기 위해 당신은'max_rows' 속성을 호출하여 대신'self.max_rows = l'을 할 수 있습니다. – tadman