# Objects

**URL:** <https://www.ruby-forum.com/t/objects/132582>\
**Category:** Ruby\
**Created:** [March 21, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/objects/132582 "2008-03-21T17:21:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HadsS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/58f4c7/32.png) [@HadsS](https://www.ruby-forum.com/u/HadsS)\
**Post date:** [March 21, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/objects/132582/1 "2008-03-21T17:21:03Z")

</div>

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…!!

---

<div class="post-metadata">

**Author:** ![HadsS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/58f4c7/32.png) [@HadsS](https://www.ruby-forum.com/u/HadsS)\
**Post date:** [March 21, 2008, 5:31pm UTC](https://www.ruby-forum.com/t/objects/132582/2 "2008-03-21T17:31:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![HadsS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/58f4c7/32.png) [@HadsS](https://www.ruby-forum.com/u/HadsS)\
**Post date:** [March 22, 2008, 1:26pm UTC](https://www.ruby-forum.com/t/objects/132582/3 "2008-03-22T13:26:01Z")

</div>

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
