Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added examples for attr_reader and attr_writer #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions source/11-writing_classes/05-attribute_readers.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ In fact they are so common that there's a word for them: they are called
"attribute readers". By "attribute" the Ruby community means an instance
variable, so an attribute reader is a method that reads an instance variable.

Let's take a look at what an attribute reader looks like based on the previous example:
```ruby
class Person
attr_reader :name

def initialize(name)
@name = name
end
end
```

<p class="hint">
An attribute reader returns the value of an instance variable.
</p>
Expand Down
11 changes: 11 additions & 0 deletions source/11-writing_classes/06-attribute_writers.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ So, yeah, we can see that, after calling `person.password=("super secret")`
the object now has an instance variable defined, i.e., the person now knows
their password, too.

Let's take a look at what an attribute writer looks like based on the previous example:
```ruby
class Person
attr_reader :name
attr_writer :password

def initialize(name)
@name = name
end
end
```
<p class="hint">
An attribute writer allows setting an instance variable.
</p>
Expand Down