# Pstore confusion for a beginner

**URL:** <https://www.ruby-forum.com/t/pstore-confusion-for-a-beginner/191729>\
**Category:** Ruby\
**Created:** [July 16, 2010, 11:18pm UTC](https://www.ruby-forum.com/t/pstore-confusion-for-a-beginner/191729 "2010-07-16T23:18:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![studlee2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@studlee2](https://www.ruby-forum.com/u/studlee2)\
**Post date:** [July 16, 2010, 11:18pm UTC](https://www.ruby-forum.com/t/pstore-confusion-for-a-beginner/191729/1 "2010-07-16T23:18:22Z")

</div>

Greetings,

I am getting my feet wet with Ruby object persistence.

I decided to look at PStore first, then move to Yaml, as it was  
mentioned in my Peter C. Ruby book.

I know that PStore is typically used for program config value  
persistence, and that the transaction keyword fairly much ensures the  
integrity of the operation, and a binary pstore file is created. Beyond  
that, I am pretty lost. I searched the forum here, and googled pstore,  
but it was not that helpful.

What I wanted to do, as a start, was to save my entire object “fdb1”,  
which is an instance of class “Faunadb”. Then, after closing Ruby, “copy  
back” the saved object, and list object variable contents, to  
demonstrate persistence conclusively. It seems to work, but I have  
questions!

The code to save the object, that I have cobbled together, without  
understanding it, is:

\<start of “copy to” code\>  
##code above here to populate the object fdb1  
store=PStore.new("/home/user1/ruby/pstorefile")  
store.transaction do  
store[:this] ||= Faunadb.new("")  
store[:this] = fdb1  
end  
\<end of “copy to” code\>

I can see the created file, and cat it though it is binary.

And the code to restore it, is:

\<start of “copy back” code\>  
fdb1=Faunadb.new("")  
store=PStore.new(/home/user1/ruby/pstorefile")  
store.transaction do  
fdb1=store[:this]  
end  
fdb1.listobs #to prove the data is back.  
\<end of “copy back” code\>

Questions:

1. In this line of code “store[:this] ||= Faunadb.new(”")" , what is the  
double pipe equal sign doing? Does it just indicate that the :this  
symbol points to the new Faunadb instance? Why is the double pipe thingy  
not used on the “copy back” code fragment? Would I use the symbol to  
just keep track of what is stored? I know :this is pretty goofy.

2. In this line of code fdb1=store[:this] from the restore, I noticed  
that it is necessary to have the same symbol :this, in this code  
fragment, as it was in the prior fragment, because I get an error  
otherwise. Is this correct?

My environment is ruby 1.8.7 on Linux.

Any tips appreciated.

Thanks in advance.  
Steve.

---

<div class="post-metadata">

**Author:** ![studlee2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@studlee2](https://www.ruby-forum.com/u/studlee2)\
**Post date:** [July 17, 2010, 2:20pm UTC](https://www.ruby-forum.com/t/pstore-confusion-for-a-beginner/191729/2 "2010-07-17T14:20:40Z")

</div>

> Questions:
> 
> 1. In this line of code “store[:this] ||= Faunadb.new(”")" , what is the  
> double pipe equal sign doing? Does it just indicate that the :this  
> symbol points to the new Faunadb instance? Why is the double pipe thingy  
> not used on the “copy back” code fragment? Would I use the symbol to  
> just keep track of what is stored? I know :this is pretty goofy.

a ||= b is short for “a = a || b”, just like “a += b” is short for “a =  
a + b”.

|| is pronounced “or” (note: Ruby has two similar operators || and  
‘or’ with an important difference – see any Ruby reference).

a ||= b is equivalent to the following code

if a

# nothing happens

else  
a = b  
end

So store[:this] ||= … is saying “if store[:this] doesn’t already  
exist, set it to…”.

> 1. In this line of code fdb1=store[:this] from the restore, I noticed  
> that it is necessary to have the same symbol :this, in this code  
> fragment, as it was in the prior fragment, because I get an error  
> otherwise. Is this correct?

A PStore object is like a hash. Imagine you used a hash to store some  
name and address details.

hash = {}  
hash[:name] = “John S.”  
hash[:address] = “…”

When you wanted to retrieve the name and address, you’d need to use  
the same keys :name and :address, wouldn’t you?

puts hash[:name]  
puts hash[:address]

Hope this helps,  
Gavin

---

<div class="post-metadata">

**Author:** ![studlee2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/278dde/32.png) [@studlee2](https://www.ruby-forum.com/u/studlee2)\
**Post date:** [July 17, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/pstore-confusion-for-a-beginner/191729/3 "2010-07-17T21:46:07Z")

</div>

On Sat, Jul 17, 2010 at 8:20 AM, Gavin S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> a ||= b is short for “a = a || b”, just like “a += b” is short for “a = a + b”.

NOT AGAIN!

[http://talklikeaduck.denhaven2.com/2008/04/26/x-y-redux](http://talklikeaduck.denhaven2.com/2008/04/26/x-y-redux)

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
