Objects

can i create objects and store them in a file once and use it other
times when i need it after reopening the program… please helpp…!!

On Mar 21, 2008, at 11:19 AM, HadsS wrote:

can i create objects and store them in a file once and use it other
times when i need it after reopening the program… please helpp…!!

You sure can. Here’s an example:

object = “whatever”

this code saves an object

File.open(“path/to/file”, “w”) { |file| Marshal.dump(object, file) }

this code loads the object back

object = File.open(“path/to/file”) { |file| Marshal.load(file) }

Hope that helps.

James Edward G. II

On 21.03.2008 17:29, James G. wrote:

File.open(“path/to/file”, “w”) { |file| Marshal.dump(object, file) }

this code loads the object back

object = File.open(“path/to/file”) { |file| Marshal.load(file) }

Hope that helps.

Just a small addition: I usually prefer to use mode “b” for marshal
files because they are binary, it helps documenting and it is more cross
platform.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs