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 and it gives you "Jane".

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


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?