...

/

Attribute Writers

Attribute Writers

This lesson briefs over attribute writers and their connection with instance variables.

Setting information

Imagine in our application a person not only needs a name, but also a password. However, let’s also imagine that, at the time of the creation of a new person instance, this password is not yet known. (Who would give a toddler an email password anyway?)

Instead we want to be able to tell the person object about its email password later.

We can do this like so:

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

Assigning Local Variable to Instance Variable

As you can see, ...

Create a free account to access the full course.

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