Why use @name, why not use just name | Codecademy (2024)

Skip to Content

This forum is now read-only. Please use our new forums! Go to forums

banner

Close banner

points

Why use @name, why not use just name | Codecademy (1)

Submitted by

dare05mkd

almost 11 years

I was wondering, when we initialize name, we use

def initialize(name) @name = nameend

Now, why do we have to put @name=name at all? Why not just use “name” as the variable and refer to it through the class? Why do we have to use a new variable called @name?

Answer 51e18db07c82caa2d1004a76

7

votes

Permalink

The @ is what makes @name an instance property, so it’s stored with the object and can be accessed by other instance methods of the same class.

If you just say name = name, then name is local to the initialize method and inaccessible anywhere outside it.

points

Why use @name, why not use just name | Codecademy (2)

Submitted by

Alex J

almost 11 years

Why use @name, why not use just name | Codecademy (2024)
Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 6701

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.