2012-04-12 2 views
1

클래스의 많은 인스턴스를 포함하는 2D 배열이 있습니다. 클래스에는 4 개의 배열이 포함되어 있습니다. 마샬을 사용하여 2D 배열을 저장하거나 디스크에서로드하고 싶습니다. 클래스를 포함하는 다른 2D 배열과 함께이 용도로 마샬을 성공적으로 사용했지만 클래스에는 배열이 포함되지 않았습니다. 여기에 문제가되는 수업의 정의가 있습니다.배열을 포함하는 Jruby에서 클래스를 마샬링하는 방법

class Light 
     attr_accessor :R,:G,:B,:A 

     def initialize(i) 

      @R = Array.new(4, i) 
      @G = Array.new(4, i) 
      @B = Array.new(4, i) 
      @A = Array.new(4, i) 

     end 

     @R 
     @G 
     @B 
     @A 

end 

나는 빛 수업 시간에 내 자신의 정렬 화 기능을 정의하는 시도 : 여기

def marshal_dump 
    {'R' => @R,'G' => @G,'B' => @B,'A' => @A} 
end 


def marshal_load(data) 
    self.R = data['R'] 
    self.G = data['G'] 
    self.B = data['B'] 
    self.A = data['A'] 
end 

이 클래스

다음
def createLightMap(width,height) 
    a = Array.new(width) { Light.new(0.7) } 
    a.map! { Array.new(height) { Light.new(0.7) } } 
    return a 
end 

@lightMap = createLightMap(10,10) 

을 포함하는 2 차원 배열의 생성 내가 저장 방법입니다 및로드

#save 
File.open('lightData','w') do |file| 
    Marshal.dump(@lightMap, file) 
end 

#load 
@lightMap = if File.exists?('lightData') 
        File.open('lightData','w') do |file| 
         Marshal.load(file) 
        end 
      else 
        puts 'no light data found' 
      end 

로드시 "in 'load'오류가 발생합니다. 덤프 형식 오류 (연결 해제, 인덱스 : -96) (인수 오류)"

사용자 지정 덤프 /로드 마샬링 기능 사용 여부에 관계없이 시도했습니다. 나는 jruby 1.5.1, ruby ​​1.8.7을 사용하고있다.

+0

클래스 선언에 맨손'@ R' 등은 무엇입니까? –

+0

그들은 C++ 코딩에서 남은 습관입니다. 나는 그들이 정말로 필요하지 않다고 생각한다? – Spencer

+0

그들은 실제로 아무 것도하지 않습니다. 클래스 선언은 순서대로 실행됩니다. 따라서 attr_accessors는 액세스 함수를 작성합니다. 'def'는 ctor 함수를 선언합니다. '@ X'는 인스턴스가 아닌 * 클래스 *의 컨텍스트에서 평가됩니다. 아무 것도 초기화되지 않았기 때문에 클래스 레벨에서 완전히 의미가 없으므로 (인스턴스가 아니기 때문에)'nil'이됩니다. 변수). –

답변

1

마샬 덤프 /로드가 문제라고 생각하지 않는다. 아마도 파일 입출력 일 것이다. 나를 위해 잘 작동합니다 (사용자 지정 마샬링없이) :

class Light 
    # You might want to downcase these variables as capitalized 
    # variables in Ruby generally denote constants 
    attr_accessor :R,:G,:B,:A 

    def initialize(i) 
    @R = Array.new(4, i) 
    @G = Array.new(4, i) 
    @B = Array.new(4, i) 
    @A = Array.new(4, i) 
    end 

    def ==(other) 
    @R == other.R && @G == other.G && @B == other.B && @A == other.A 
    end 
end 

# Method names are generally underscored/snake cased 
# (JRuby is even smart enough to map this to Java's camel casing). 
# Also should probably attach this method to a class or module to prevent 
# polluting global namespace 
def create_light_map(width,height) 
    a = Array.new(width) { Light.new(0.7) } 
    # Note you don't need explicit returns - the last value evaluated is the return value 
    a.map { Array.new(height) { Light.new(0.7) } } # You can also lose the ! on map 
end 

# Same casing style applies to variables 
light_map = create_light_map(10,10) 
# => [[#<Light:0x5ec736e4 @A=[0.7, 0.7, 0.7, 0.7], ... 

# Note with marshaled data you should probably open file in binary mode 
File.open('/tmp/lightData','wb') { |f| f.write(Marshal.dump(light_map)) } 
# => 5240 

light_map_demarshaled = File.open('/tmp/lightData','rb') { |f| Marshal.load(f.read) } 
# => [[#<Light:0x6a2d0483 @A=[0.7, 0.7, 0.7, 0.7], ... 

light_map_demarshaled == light_map 
# => true 
+1

당신은 파일 IO에 대해 완전히 옳았습니다. 클래스의 마샬 덤프 (marshal dump) 및로드 함수를 작성한 후에 변수의 이름을 잘못 지정한 것으로 수정해야합니다. 방금 그들을 제거하고 잘 작동합니다. 내 코드를 정리 해줘서 고마워! – Spencer

관련 문제