# Newbie question: how to read the ":"

**URL:** <https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78046>\
**Category:** Rails\
**Created:** [December 3, 2006, 12:06am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78046 "2006-12-03T00:06:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jake\_Barnes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@Jake\_Barnes](https://www.ruby-forum.com/u/Jake_Barnes)\
**Post date:** [December 3, 2006, 12:06am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78046/1 "2006-12-03T00:06:49Z")

</div>

I’m a total beginner at Ruby. I’m trying to read this code:

private  
def find\_cart  
session[:cart] ||= Cart.new  
end

I’m most comfortable with PHP code, so that biases how I read this. If  
I had to guess, I’d say this is saying something like “Let’s create a  
private method called find\_cart. When called, this should check the  
session array for a key called ‘cart’. If there is no key called ‘cart’  
in the session array yet, then we create a new Cart object and store it  
in the session array with a key of ‘cart’.”

How close did I get?

---

<div class="post-metadata">

**Author:** ![Jake\_Barnes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@Jake\_Barnes](https://www.ruby-forum.com/u/Jake_Barnes)\
**Post date:** [December 3, 2006, 1:45am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78046/2 "2006-12-03T01:45:34Z")

</div>

You’re spot on, except session is a Hash, not an Array.

---

<div class="post-metadata">

**Author:** ![Jake\_Barnes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@Jake\_Barnes](https://www.ruby-forum.com/u/Jake_Barnes)\
**Post date:** [December 3, 2006, 2:09am UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78046/3 "2006-12-03T02:09:37Z")

</div>

Hi –

On Tue, 21 Nov 2006, Jake Barnes wrote:

> I had to guess, I’d say this is saying something like “Let’s create a  
> private method called find\_cart. When called, this should check the  
> session array for a key called ‘cart’. If there is no key called ‘cart’  
> in the session array yet, then we create a new Cart object and store it  
> in the session array with a key of ‘cart’.”
> 
> How close did I get?

Very, but the key is the symbol :cart, rather than the string  
‘cart’… and the only reason that doesn’t matter is that Rails  
performs some modifications on the Hash class so that symbols and  
strings can be used interchangeably as keys. In un-modified Ruby  
code, :cart and “cart” are completely different hash keys.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Jake\_Barnes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@Jake\_Barnes](https://www.ruby-forum.com/u/Jake_Barnes)\
**Post date:** [December 3, 2006, 8:23pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78046/4 "2006-12-03T20:23:20Z")

</div>

Hi Jakob,

Jakob S. wrote:

> check the session array for a key called ‘cart’.

You probably already know this, but just to make sure…

In Rails, session data is stored as a hash, not an array.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![Jake\_Barnes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f17d59/32.png) [@Jake\_Barnes](https://www.ruby-forum.com/u/Jake_Barnes)\
**Post date:** [December 3, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/newbie-question-how-to-read-the/78046/5 "2006-12-03T20:03:17Z")

</div>

On Nov 22, 2006, at 00:03 , Jake Barnes wrote:

> ‘cart’  
> in the session array yet, then we create a new Cart object and  
> store it  
> in the session array with a key of ‘cart’."
> 
> How close did I get?

Bingo, good job. That’s pretty much exactly what happens.

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)
