Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to marshal a class in Jruby which contains an array
    primarykey
    data
    text
    <p>I have a 2D array containing many instances of a class. The class contains 4 arrays. I would like to save and load the 2D array to/from disk using Marshal. I have successfully used Marshal for this purpose with other 2D arrays containing classes, but those classes did not contain an array. Here is the definition of the class giving me trouble.</p> <pre><code>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 </code></pre> <p>I have tried defining my own marshal functions in the Light class:</p> <pre><code>def marshal_dump {'R' =&gt; @R,'G' =&gt; @G,'B' =&gt; @B,'A' =&gt; @A} end def marshal_load(data) self.R = data['R'] self.G = data['G'] self.B = data['B'] self.A = data['A'] end </code></pre> <p>Here is the creation of the 2D array containing this class</p> <pre><code>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) </code></pre> <p>Here is how I save and load</p> <pre><code>#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 </code></pre> <p>Upon load, I receive the error "in 'load': dump format error (unlinked, index: -96) (Argument Error)"</p> <p>I have tried with and without custom dump/load marshal functions. I am using jruby 1.5.1, ruby 1.8.7</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload