Chevron Left Icon
Object-Oriented Programming II
Lesson 1 of 2
Chevron Right Icon
  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.
  1. 1
    All right! Now that you know how to control the level of privacy in a Ruby class, we can use that to our advantage when creating objects. In this case, we’ll be making an Account object with public…
  2. 2
    We’ll start off by creating our Account class. First, though, you probably noticed this bit of fanciness in the last exercise: def initialize(name, balance=100) @name = name @balanc…
  3. 3
    Let’s go ahead and create our private methods. (It makes sense to create these first, since our public methods will rely on them.)
  4. 4
    Perfect! Now let’s tackle the first public method, display_balance, which we’ll use to display our current account balance.
  5. 5
    Well done! Now let’s add in our second public method, which will allow us to withdraw money from our account. The trick to this one is to realize that since @balance can only be accessed from insi…
  6. 6
    We’re almost there! Now we just need to create an instance of Account so we can go about our banking business.
  7. 7
    Excellent work! You’ve got your very own Account class and checking_account instance, complete with public and private methods. Go ahead and try to get to your pin from outside the class. Not easy…