Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Modules Store Inventory Using Modules Creating an In Stock Report

gene c
gene c
13,630 Points

Why are all instances declared and assigned to the same name 'shirt' or 'pant' instead of 'shirt1', 'shirt2' etcetc?

won't that override previous existing data?

1 Answer

Matt Campbell
Matt Campbell
1,012 Points

An instance variable is a variable that can be seen by all methods in that particular instance.

So if you are talking about a Person object. And that particular object has attributes like name, shirt, pants, shoes, socks, etc, those particular variables would be instance variables.

Then if you have 3 people, each person has a name. Jane, Joe, Sally. So if you look at the first person, if you want to know their name, you just call person.name and it gives you "Jane".

The second person, you call person.name and it gives you "Joe".

Etc.

So the same would be said for shirt. They all might be different shirts, but each person only has one shirt. So you don't need to call person.shirt1, you just need to call person.shirt because that shirt belongs specifically to that person.

So if you aren't scoped to a particular person, you can't just ask what "shirt" is because "shirt" is only available in that instance of the person. It's not a global variable that's accessible everywhere.

Does that make sense?