# Basic Ruby question

**URL:** <https://www.ruby-forum.com/t/basic-ruby-question/83076>\
**Category:** Rails\
**Created:** [January 23, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/basic-ruby-question/83076 "2007-01-23T15:45:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [January 23, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/basic-ruby-question/83076/1 "2007-01-23T15:45:40Z")

</div>

hi all,

Understanding function has product = Product.find(params[:id]) and  
@cart = find\_cart . Correct me if I’m wrong but I think you always need  
a @ before a variable? product hasn’t got this. Does this mean product  
isn’t a variable?

What is the difference between product and @cart.

Thanks  
Stijjn

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
end

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [January 23, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/basic-ruby-question/83076/2 "2007-01-23T16:11:43Z")

</div>

Tarscher wrote:

> hi all,
> 
> Understanding function has product = Product.find(params[:id]) and  
> @cart = find\_cart . Correct me if I’m wrong but I think you always need  
> a @ before a variable? product hasn’t got this. Does this mean product  
> isn’t a variable?

Putting @ in front of a variable makes it an instance variable accesible  
by other methods of that class (except class/singular methods). Without  
it, it’s just local (from the point it’s defined) till the end of the  
current scope.

Your confusion probably stems from a little trick Rails incorporates…  
It moves all instance variables from a view’s controller to the  
correspoding view. So in the example above @cart will be accesible by  
the view when it’s being rendered (as well as other methods in the  
controller)

I recommend that you pick up a book on Ruby as it will greatly enhance  
your experience and appreciation of Rails…

Hope this helps…

ilan

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [January 23, 2007, 4:20pm UTC](https://www.ruby-forum.com/t/basic-ruby-question/83076/3 "2007-01-23T16:20:46Z")

</div>

You don’t always need a @. In fact, you don’t always need punctuation of  
any kind in Ruby!  
The @ makes the variable an instance variable. They can be used to share  
a value between different methods in the same class, such as inside one  
of your controllers in rails, or between a controller and its views.

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [January 23, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/basic-ruby-question/83076/4 "2007-01-23T16:23:49Z")

</div>

Hi Stijjn,

Tarscher wrote:

> What is the difference between product and @cart.  
> def add\_to\_cart  
> product = Product.find(params[:id])  
> @cart = find\_cart  
> @cart.add\_product(product)  
> end

product is a local variable. Its value is only available for use within  
the  
add\_to\_cart method. @cart is an instance variable. It’s value is  
available  
for use within all the methods in the controller and their views for the  
duration of the request/response cycle in which the variable was  
instantiated.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [January 23, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/basic-ruby-question/83076/5 "2007-01-23T16:30:52Z")

</div>

thanks all for the quick replies!

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [January 23, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/basic-ruby-question/83076/6 "2007-01-23T16:26:50Z")

</div>

On Jan 23, 2007, at 9:42 AM, Tarscher wrote:

> Thanks  
> Stijjn
> 
> def add\_to\_cart  
> product = Product.find(params[:id])  
> @cart = find\_cart  
> @cart.add\_product(product)  
> end

product is a local variable  
@cart is an instance variable

On big difference for Rails has to do with the “magic” that injects  
the instance variables from a controller into the view being  
rendered. If this add\_to\_cart method is in a controller, the app/  
view//add\_to\_cart.rhtml will have access to @cart, but  
not product.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [January 23, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/basic-ruby-question/83076/7 "2007-01-23T16:35:52Z")

</div>

Tarscher:

“product” is a local variable. It’s scope is local to the method in  
which it is being used. Once the method is finished executing,  
“product” ceases to exist.

@cart is an instance variable. It’s scope is the class in which it is  
being used. If the add\_to\_cart method is defined in the controller,  
then @cart will exist throughout the life of the controller. Instance  
variables of controllers are accessible from the view. So, @cart  
could be shown on a result page; “product” cannot.

I have two friendly suggestions born from my own experience learning  
Rails. First, get a few Ruby-specific books. They will help with  
these types of questions, act as references for functions, and the  
more code you read from multiple sources, the better your code will  
become. Second, in case add\_to\_cart from below is defined in your  
controller, consider reading more about test driven development (TDD)  
and try not to put business logic into the controller. Put business  
logic in model objects and write unit tests for each model object. I  
know it sounds like more work, but it will save you so much time  
later on when you start modifying things.

Hope it helps,

-Anthony
