Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7788255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:56:19+00:00 2026-06-01T20:56:19+00:00

I have a 2D array containing many instances of a class. The class contains

  • 0

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.

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

I have tried defining my own marshal functions in the Light class:

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

Here is the creation of the 2D array containing this class

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)

Here is how I save and load

#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

Upon load, I receive the error “in ‘load’: dump format error (unlinked, index: -96) (Argument Error)”

I have tried with and without custom dump/load marshal functions. I am using jruby 1.5.1, ruby 1.8.7

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:56:20+00:00Added an answer on June 1, 2026 at 8:56 pm

    I don’t think it’s the Marshal dump/load that is the problem, it’s probably just the file I/O. This works fine for me (without custom marshaling):

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to have an array of arrays in a class using Zend and
I have a 1d array containing Nd data, I would like to effectively traverse
so i have an array containing many instances. let's say movieclips. and i have
I have an int array containing gray scale values from 0-254, i also have
I have a 2-d array containing pairs of values and I'd like to make
I have an Array in PHP which contains data from database. And it prints
I have an array of arrays, each array containing details of a scan by
Groups has_ many users. I have 2 activerecord arrays: 1. groups = containing groups
Hi I have an NSArray containing many 'multimedia action' as I like to call
I have a array containing japanese caracters as well as normal. How do I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.