...

/

Instance Variables

Instance Variables

A detailed description of instance variables and how they work in Ruby.

An object’s own knowledge

Now that you understand how the string that we pass to the method new ends up being passed to the new object’s initialize method, we can start improving initialize, so it does something with the string, and actually initializes our new object:

Press + to interact
class Person
def initialize(name)
@name = name
end
end

This introduces another new concept: @name is a new type of variable, called an “instance variable”.

Assigning Local Variable to an Instance Variable

The body ...

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy