2012-04-12 19 views
1

我有一個包含許多類的實例的2D數組。該類包含4個數組。我想用Marshal保存和加載二維數組到/從磁盤。我已經成功地將Marshal用於其他包含類的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) 

這裏的二維數組的創建是我如何保存和負載

#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'加載':轉儲格式錯誤(未鏈接,索引:-96)(參數錯誤)」

我試過了,沒有自定義轉儲/加載編組函數。我使用jruby 1.5.1,紅寶石1.8.7

+0

什麼是那些裸'@ R'等等的東西在類聲明? – 2012-04-12 17:19:35

+0

他們是從C++編碼中剩下的習慣。我猜他們不是很需要? – Spencer 2012-04-12 17:21:19

+0

他們實際上不會做任何事情;類聲明按順序執行。所以attr_accessors創建訪問函數。 'def'聲明瞭ctor函數。 「@ X」只會在* class *的上下文中評估,而不是實例,因此它們沒有被初始化爲任何東西,而是「nil」,並且在類級別上完全沒有意義(因爲它們是實例變量)。 – 2012-04-12 17:38:37

回答

1

我不認爲這是元帥轉儲/負載是問題,它可能只是文件I/O。這對我很好(沒有自定義封送):

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完全正確。我必須命名一個變量錯誤或什麼東西,並在編寫該類的元帥轉儲和加載函數之後修復它。我只是刪除它們,它工作得很好。謝謝你讓我清理我的代碼! – Spencer 2012-04-14 18:04:25

相關問題