Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Ruby Programming
Previous Page Home Next Page

Marshaling and Distributed Ruby

Java features the ability to serialize objects, letting you store them somewhere and reconstitute them when needed. You might use this facility, for instance, to save a tree of objects that represent some portion of application state---a document, a CAD drawing, a piece of music, and so on.

Ruby calls this kind of serialization marshaling.[Think of railroad marshaling yards where individual cars are assembled in sequence into a complete train, which is then dispatched somewhere.] Saving an object and some or all of its components is done using the method Marshal::dump . Typically, you will dump an entire object tree starting with some given object. Later on, you can reconstitute the object using Marshal::load .

Here's a short example. We have a class Chord that holds a collection of musical notes. We'd like to save away a particularly wonderful chord so our grandchildren can load it into Ruby Version 23.5 and savor it, too. Let's start off with the classes for Note and Chord.

class Note
  attr :value
  def initialize(val)
    @value = val
  def to_s

class Chord   def initialize(arr)     @arr = arr   end   def play     @arr.join('-')   end end

Now we'll create our masterpiece, and use Marshal::dump to save a serialized version of it to disk.

c = ["G"),"Bb"),
       "Db"),"E") ] )"posterity", "w+") do |f|   Marshal.dump(c, f) end

Finally, our grandchildren read it in, and are transported by our creation's beauty."posterity") do |f|
  chord = Marshal.load(f)
end "G-Bb-Db-E"

Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire