From: mirian@... (Mirian Crzig Lennox) Date: 2001-06-10T10:33:54+09:00 Subject: [ruby-talk:16378] Re: other ways to create class instance? In article <3B22BD76.3E04270A@objectent.com>, Samantha Atkins wrote: >Hi, > >I was fooling around with object persistence ideas in Ruby and >came to a question I haven't found an answer to yet. How can I >create an instance of a class X without invoking X.new(...)? >What I need is to be able to create an instance with the right >plumbing set up and hand set instance variables from data >retrieved for the persistent form of the object. So how would >you do that? Possibly the marshalling support in Ruby is what you're after. It allows you to specify a class method X._load() which loads an object of class X from persistent store. You can handle all of your custom plumbing inside that method, which will however probably need to begin with a call to X.new() to allocate the initial version of that object to fiddle with. You can also define a method _dump() on that object to write it out to persistent store. The Marshal module takes care of calling these hooks, and can also write objects to persistent store by means of a default protocol for classes which don't define _load() and dump(). Pages 272-274 of the Pickaxe book describe marshalling in Ruby. --Mirian