Join Kevin Skoglund for an in-depth discussion in this video Class reader/writer methods, part of Ruby: Classes and Modules.
- [Instructor] In this movie, we're going to learn about class reader/writer methods. In the previous movie we learned about the instance reader/writer methods, and class reader/writer methods are very similar. But there is no equivalent to those attr methods that we worked with, where we had attr_reader, attr_writer and attr_accessor. There's nothing like that for classes. Now, in Ruby on Rails, the web framework, there is such a thing, and it's cattr_, that lets you know that it's a class attribute, not an instance one, but a class one.
So that c in front is very important, but Ruby does not, at least not yet. Sometimes things that become popular in Ruby on Rails make their way back into Ruby, but it hasn't happened so far. Why not? Well, they're just not needed that often. It's not very often you're going to use class variables, to begin with, and even less frequently that you need to read and write to them. But if you do, it's easy enough for you to define them yourself, just like we did with the instance reader and writer. But say for example, that we have our Animal class and we have our species class variable.
We already saw in the last movie how we can read from that value by just defining a class method, self.species, and it returns the value of that class variable. Nice and easy, we have a reader method. If we want a writer method, it works exactly the same way that it does with an instance one, we just have self in front of it. So self., and then the method name with an equals sign that gives us that nice syntactic sugar syntax that makes it look like we're just assigning a value to it, even though we're actually calling a method name.
In the example I have here, you see I'm providing an array as the argument. In the first line I just make sure that what I'm being passed actually is of the class array. If it is, then I'll assign it to the species class variable. That's it, that's all there is to being able to create class reader and writer methods. There's just not a shortcut for them. Let's try adding an example for our species. So we have species reader already created, that's exactly how that would work. Let's create species writer using species= and then whatever we want to pass in.
I'm going to do the same thing that I did in my example. I'm going to return unless the array.is_a?, and then the class name, Array. So return unless you've been given an array, but if you have an array, let's go ahead and assume that it's good. Species is going to be equal to whatever array you pass in. So let's try it out. Let's go back over here into our code, and we already have our species up here that we're listing the first time. Let's come down here and let's try Animal.species =, and let's set it equal to a different array with something different.
Let's try cat and mouse and moose. All right, and then let's just have it return the value there. Actually, we'll just go up here and copy this line, three lines, and we'll have it output it again. And just to keep it separate, we'll just also copy this little break. All right, let's save it and let's try and run it. So from the root of my ruby_project, ruby init.rb, runs my file, and there we are.
Cat, mouse and moose. See, it was able to both write the values and to read them back, using the class itself, not an instance, but the class as a whole.
- Defining a custom class
- Working with instances of a class
- Class and instance attributes and methods
- Class inheritance
- Working with dates and times
- Using modules as namespaces and mixins
- Raising and handling exceptions