Subclasses, Constants and Namespacing!
Still here! Still plugging along. I haven't been posting as much since I've been distracted by other projects and life updates, but also because I have felt hesitant to post anything related to my app academy application.
But I wanted to mention some things I've realized yesterday in my studies. Hopefully it is helpful for new rubyists.
I was working on an exercise about subclasses in ruby and kept getting weird error messages about uninitialized constants, which confused me since I thought constants were more like permanent variables (like Pi for instance). Well apparently anything that starts with an uppercase (including classes and subclasses!) are constants in ruby! Which I guess makes sense because you probably want some sort of constant definition of your classes, but for some reason I missed that boat and spent too much time reading about constants when I should have been reading about namespacing. I don't know if I just missed this or forgot it but just in case you get such error messages, fear not!
Next why was I getting this error message?
Here's a pretend pseudo-code example of what I was doing:
Class Directions
Class Right
righty_stuff
end
Class Left
lefty_stuff
end
end
Why are my constants (or in this case subclasses) uninitialized? Because I was declaring them within the class and calling them outside of it. Now if you're like me you think that it would only be reasonable to type out my Right class within the Directions class. Which is a pretty reasonable assumption. But when my rspec called it, it was looking for something like Right.blah. Unfortunately computers can be stupid and they don't know which super class to look for Right. So rather than look around, it pretended it didn't exist. So the answer here was to copy and paste my Right and Left subclasses outside of the Directions class and say that it < Directions.
The way I like to think of this is that sub classes are not necessarily trapped under the management of super classes, but they just inherit all of their features. Like a child, a subclass can live with their parents or they can move out. But they still inherit their DNA. So when would you want a subclass under the parental roof? And how do you access such things without funky error messages?
Well what if we had a weird program where we had our same directions class but we also had something else:
Class Directions
Class Right
righty_stuff
end
Class Left
lefty_stuff
end
end
Class Correctness
Class Right
much_true_stuff
end
Class Wrong
much_incorrect_you_silly
end
end
In this case we have 2 different Right classes and we need them to be in their parental super classes so that we don't confuse the computer when we call them. This is called namespacing! But to access it? This is where that confusing and scary looking :: comes in. You can call it with Directions::Right or Correctness::Right. If you just use Right, Ruby will pretend it's never heard of the kid and will call him an uninitialized Constant.
Anyway I'm sure I read this all somewhere in Ruby monk or somewhere else but I don't know if I understood it before now. (hopefully I've got it right this time!)







