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 Foundations Objects, Classes, and Variables Objects and Classes

I don't fully understand the initialization at the beginnin. Why do we have instance variable called @transaction = [] ?

What @transaction = [] does?

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

The initialize method runs every time you create a new object of this given class. It usually creates some variables that this class will use to track various things. It INITIALIZES them - creates their copies for the given instance of the class (hence - instance variables - variables available for all the methods within this instance of the object).

The @transactions variable is not used in this video, but in the future it can be used to track all the transactions that take place within the given bank account (since it's in the initialize method, every bank account will get an empty array for storing transaction history once it is created). Think about your own bank account - you can always look at the history of transactions, stating the date, the amount, the receiver etc. This instance variable is initiated as an empty array, wchich will make it easy to insert new transactions every time they are made using the << operator.