Objects

H

HadsS

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...!!
 
J

James Gray

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 Gray II
 
R

Robert Klemme

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top