This forum is now read-only. Please use our new forums! Go to forums
banner
Close banner
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
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.
Popular free courses
Free Course
Learn SQL
Beginner friendly,
4 Lessons
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
Language Fluency
Free Course
Learn JavaScript
Beginner friendly,
11 Lessons
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
Language Fluency
Free Course
Learn HTML
Beginner friendly,
6 Lessons
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
Language Fluency