# Noob syntax question

**URL:** <https://www.ruby-forum.com/t/noob-syntax-question/172391>\
**Category:** Rails\
**Created:** [July 20, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/noob-syntax-question/172391 "2009-07-20T23:54:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![internetchris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@internetchris](https://www.ruby-forum.com/u/internetchris)\
**Post date:** [July 20, 2009, 11:54pm UTC](https://www.ruby-forum.com/t/noob-syntax-question/172391/1 "2009-07-20T23:54:39Z")

</div>

Hey everyone,

I’m just solidifying my understanding of ruby “example by example”.  
Can someone let me know if I understand this correctly…

In my view I have the following…

\<%= render :partial =\> “sidebar” ,  
:locals =\> { :recent\_messages =\> @recent\_messages } %\>

Am I correct when I say that I’m passing the “@recent\_messages” as an  
instance variable to the new partial? For example, in the new partial  
if I did this…

\<%= render :partial =\> “sidebar” ,  
:locals =\> { :foo =\> @recent\_messages } %\>

Then “:foo” would be the instance variable that is available for use  
in the partial. I’m simply passing the @recent\_messages to that  
instance variable name correct?

I just want to be sure I don’t have that backwards. I keep thinking  
that @recent\_messages is an instance variable, but it’s actually an  
@variable.

Thanks!

Chris

---

<div class="post-metadata">

**Author:** ![internetchris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@internetchris](https://www.ruby-forum.com/u/internetchris)\
**Post date:** [July 21, 2009, 12:09am UTC](https://www.ruby-forum.com/t/noob-syntax-question/172391/2 "2009-07-21T00:09:16Z")

</div>

Hi –

On Mon, 20 Jul 2009, internetchris wrote:

> :locals =\> { :recent\_messages =\> @recent\_messages } %\>  
> instance variable name correct?
> 
> I just want to be sure I don’t have that backwards. I keep thinking  
> that @recent\_messages is an instance variable, but it’s actually an  
> @variable.

@recent\_messages is an instance variable. Anything identifier that  
starts with a single ‘@’ is an instance variable.

When you do this:

:locals =\> { :foo =\> @recent\_messages }

your setting this up so that in your partial, you will have a _local_  
variable called foo, initialized to the value of @recent\_messages. So  
you can then do things like:

\<% foo.each do |message| %\>  
…  
\<% end %\>

However, since @recent\_messages is an instance variable, it’s actually  
going to be visible in the partial already. In other words, you can  
drop that :foo thing and do this in the partial, if you want:

\<% @recent\_messages.each do |message| %\>

and so forth.

So there are two things going on here: the instance variable itself  
(which is visible in the master template and the partial), and the  
local variable “foo” in the partial, which you have initialized to the  
same value as @recent\_messages. If you use “recent\_messages” instead  
of “foo”, then you’ll get a local variable of that name in the  
partial.

David

–  
David A. Black / Ruby Power and Light, LLC  
Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
Now available: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://manning.com/black2))  
Training! Intro to Ruby, with Black & Kastner, September 14-17  
(More info: [http://rubyurl.com/vmzN](http://rubyurl.com/vmzN))

---

<div class="post-metadata">

**Author:** ![internetchris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c68b51/32.png) [@internetchris](https://www.ruby-forum.com/u/internetchris)\
**Post date:** [July 21, 2009, 12:31am UTC](https://www.ruby-forum.com/t/noob-syntax-question/172391/3 "2009-07-21T00:31:29Z")

</div>

Many thanks!

I did have it backwards. I felt uncomfortable describing it to myself  
so I thought I better ask.

Chris
