Object-Oriented Programming, Part II

TBD

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]
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! [...] 1. First we tell Ruby that we want to construct our class [...] . Don't forget the [...] to end your class! 2. Inside our [...] is where we define our methods. ...

  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 [...] or [...] , your methods will be public. In this ca...

  4. 4

    Perfect! But not all our Ruby methods should be public. Just as we use [...] to announce our public methods, we use [...] to specify our private ones: [...] [...] methods are just that: t...

  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 [...] instance variable, we had to write something like [...] Well,...

  6. 6

    If we want to both read and write a particular variable, there's an even shorter shortcut than using [...] and [...] . We can use [...] to make a variable readable and writeable in one fell ...

  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 [...] keyword, like so: [...] Like class names,...

  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 [...] and [......

  10. 10

    Perfect! Some modules, like [...] , are already present in the interpreter. Others need to be explicitly brought in, however, and we can do this using [...] . We can do this simply by typing [....

  11. 11

    We can do more than just [...] a module, however. We can also [...] it! Any class that [...] s 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 [...] mixes a module's methods in at the instance level (allowing instances of a particular class to use the methods), the [...] keyword mixes a module's methods at the class level. T...

  15. 15

    All right! Let's do a little practice with [...] 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 [...] object with public...

  2. 2

    We'll start off by creating our [...] class. First, though, you probably noticed this bit of fanciness in the last exercise: [...] What's that [...] doing? It's signifying an **optional param...

  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, [...] , 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 [...] can only be accessed from insid...

  6. 6

    We're almost there! Now we just need to create an instance of [...] so we can go about our banking business.

  7. 7

    Excellent work! You've got your very own [...] class and [...] instance, complete with public and private methods. Go ahead and try to get to your [...] from outside the class. Not easy, is ...

Object-Oriented Programming, Part II

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]