Key Concepts

Review core concepts you need to learn to master this subject

Ruby attr_accessor Method

class CollegeStudent attr_reader :dorm attr_accessor :major def initialize(dorm, major) @dorm = dorm @major major end end #In this example, Ruby is able to only read the @dorm instance variable but both read and write the @major instance variable since it was passed to the attr_accessor method.

In Ruby, attr_accessor, used to make a variable both readable and writeable, is a shortcut to attr_reader and attr_writer.

Object-Oriented Programming II
Lesson 1 of 2
  1. 1
    You might be wondering why we need to hide information in Ruby. Isn’t it okay for every part of your Ruby program to know what every other part is doing? Possibly, if you’re the only one writing a…
  2. 2
    Let’s build a class! class ClassName def method_name(parameter) @class_variable = parameter end end 1. First we tell Ruby that we want to construct our class ClassName. Don’t forget the…
  3. 3
    Great work! Now we’ll get into the details of public and private methods. Methods are public by default in Ruby, so if you don’t specify public or private, your methods will be public. In this cas…
  4. 4
    Perfect! But not all our Ruby methods should be public. Just as we use public to announce our public methods, we use private to specify our private ones: class ClassName # Some class stuff pu…
  5. 5
    We saw in the lesson on classes that Ruby needs methods in order to access attributes. For instance, if we want to access a @name instance variable, we had to write something like def name @nam…
  6. 6
    If we want to both read and write a particular variable, there’s an even shorter shortcut than using attr_reader and attr_writer. We can use attr_accessor to make a variable readable and writeabl…
  7. 7
    You can think of a module as a toolbox that contains a set methods and constants. There are lots and lots of Ruby tools you might want to use, but it would clutter the interpreter to keep them …
  8. 8
    You can pull in pre-existing modules (we’ll show you how soon!), but you can also make your own. Modules are super easy to make! You just use the module keyword, like so: module ModuleName # Bits…
  9. 9
    One of the main purposes of modules is to separate methods and constants into named spaces. This is called (conveniently enough) namespacing, and it’s how Ruby doesn’t confuse Math::PI and Circ…
  10. 10
    Perfect! Some modules, like Math, are already present in the interpreter. Others need to be explicitly brought in, however, and we can do this using require. We can do this simply by typing requir…
  11. 11
    We can do more than just require a module, however. We can also include it! Any class that includes a certain module can use those module’s methods! A nice effect of this is that you no longer ha…
  12. 12
    What we did in the last exercise might not have seemed strange to you, but think about it: we mixed together the behaviors of a class and a module! When a module is used to mix additional behavior…
  13. 13
    Now you understand why we said mixins could give us the ability to mimic inheriting from more than one class: by mixing in traits from various modules as needed, we can add any combination of behav…
  14. 14
    Whereas include mixes a module’s methods in at the instance level (allowing instances of a particular class to use the methods), the extend keyword mixes a module’s methods at the class level. Th…
  15. 15
    All right! Let’s do a little practice with public methods.
  16. 16
    Good! Now let’s try a private method.
  17. 17
    Super. Let’s move on to modules!
  18. 18
    Almost there! Our review wouldn’t be complete without a little mixin magic.