From 3928ca7ca3287c8fe87edb2185fbabe05a70f91d Mon Sep 17 00:00:00 2001 From: Pratik Sampat Date: Wed, 17 Jan 2018 12:49:55 -0600 Subject: [PATCH] Added examples for attr_reader and attr_writer --- source/11-writing_classes/05-attribute_readers.md | 11 +++++++++++ source/11-writing_classes/06-attribute_writers.md | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/source/11-writing_classes/05-attribute_readers.md b/source/11-writing_classes/05-attribute_readers.md index 689c36d..4824863 100644 --- a/source/11-writing_classes/05-attribute_readers.md +++ b/source/11-writing_classes/05-attribute_readers.md @@ -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 +``` +

An attribute reader returns the value of an instance variable.

diff --git a/source/11-writing_classes/06-attribute_writers.md b/source/11-writing_classes/06-attribute_writers.md index 6b43cfc..9857d75 100644 --- a/source/11-writing_classes/06-attribute_writers.md +++ b/source/11-writing_classes/06-attribute_writers.md @@ -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 +```

An attribute writer allows setting an instance variable.