Monday, October 27, 2014

Classes, or How To Make Your Own Mick Jagger

The first 9 chapters or so of the Chris Pine were pretty familiar to me conceptually. I'd seen much of that material in an online Python course I took on Coursera (which I totally recommend, btw), so I'd mostly been learning new syntax, brushing up, and practicing.

But then I slammed into a brand new concept! It was, as Aladdin and Jasmine once said, a whole new world. A new, fantastic point of view

I'm talking about classes. Hark!


1. What is a class? Why do we use it?

In a sense, a class is like a *blueprint* for certain objects. You're telling your application that you have a certain type of object that you're going to use, and that all of the objects of this type have certain properties - things they can do and can be done unto them. This is organizationally helpful when you start to write more complicated applications - if you have lots of objects interacting with lots of methods, for instance. Classes keep your objects and methods organized and saves you from having to repeat yourself in a lot of different places.

Consider strings, arrays, integers, etc. These are classes built into Ruby and we can call certain methods on each of these classes. For example, if you have the string "meow meow", you can call the length method on it ("meow meow".length) to get how many characters the string is. It's not so much that classes are a brand new thing - I've been working with them this whole time. It's the idea that you can and should make your own. Exciting!

An important distinction is that a class you create is different from the objects you create. is A class is a type of object, like a string, whereas a new object of that class, is an instance of that class. If you have class Kitty (because, why would you not?), each new kitty you create is an instance of Kitty, just as Brangelina might be an instance of the class CelebCouples. So, if you make Mr. Mittens, she is an instance of the class, and we would call her a kitty object or an object of class Kitty.


2. How do we use classes?


Here's how you would create this fabled Kitty class. You want a file for your class separate from the main workings of your program. In that file (kitty.rb, maybe), your code starts out with:

class Kitty

     #write some methods in here

end

The methods you write are what define what your objects can do or have done to them. Somebody somewhere had to create the string class, back at the dawn of time, and their string.rb file would have methods for splitting, changing the case, etc. It would have also had a method for setting the initial conditions for the string. We want to do that with Kitty as well. I might look like:

class Kitty

     def initialize(name, breed)
          @name = name
          @breed = breed
     end

end

For our Kitty class, our initialize method has the parameters name and breed, and the method assigns those to a variable. You might notice that the variable name has an @ symbol before it. This signifies that the variable is an instance variable, that is, a variable that is true for the entire instance. This means that the @name variable can be used in any method in the class without it screwing everything up. If you were working with local variables, you could only use that variable inside the initialize method, which would really limit what you could do with your class.



I'll add a few more methods in there:

class Kitty
   
     def initialize(name, breed)
          @name = name
          @breed = breed
     end

     def name
          @name
     end

     def breed
          @breed
     end

     def talk
          puts "meow"
     end

     def pet
          puts "purr purr purr"
     end

end

Okay, that's a lot of stuff. Check out the first two first. These methods look like they don't do anything, right? And don't you already know what @name and @breed are from the initialize method? Well, not exactly. The name and breed methods don't assign anything - what they do is make it so that information is accessible to the user. You now have a method you can call that simply tells you any particular kitty's name and one that tells you any particular kitty's breed. That's handy info to have access to.

The talk method is a simple thing that a kitty can do. You call the talk method and the output of that will be
>> meow

The pet method is a simple thing a kitty can have done unto them. You call the pet method and the output would be
>>purr purr purr

Next thing to talk about is how you actually do an instantiation and call other methods. The quick and dirty way to check this out is to get into irb in your terminal window, by navigating to the directory your kitty.rb file is in [add link to CLI tutorial], then type 'irb' at the command prompt.

Once you're in irb, you can load your file:

$ load 'kitty.rb'

Once you've done this, you're ready to instantiate yourself a brand new kitty. Remember that the initialize method needs you to pass in a name and a breed. I have always wanted a tabby named Mick Jagger (because, why would you not?), so here's how I would realize my dreams:

$ cat = Kitty.new("Mick Jagger", "tabby")

And blammo! Mick Jagger the tabby exists! But let's say I contract some rare form of selective amnesia, and I momentarily forget the glorious name of this cat. Never fear! All I have to do is:

$ cat.name
>>Mick Jagger

$ cat.breed
>>tabby

Sweet. Now, I can enter cat.talk or cat.pet to make Mick Jagger meow and purr. I need nothing else from this life.

No comments:

Post a Comment