Exercise 42: Gothons Are Getting Classy
While it's fun to put functions inside of hashes, you'd think there'd be
something in Ruby that does this for you. There is: the
class is how you create an even more awesome "hash
with functions" than the one you made in the last exercise. Classes have
all sorts of powerful features and uses that I could never go into in
this book. Instead, you'll just use them like they're fancy
A programming language that uses classes is called "Object Oriented Programming". This is an old style of programming where you make "things" and you "tell" those things to do work. You've been doing a lot of this. A whole lot. You just didn't know it. Remember when you were doing this:
stuff = ['Test', 'This', 'Out'] puts stuff.join(' ')
You were actually using classes. The variable stuff is actually an Array
stuff.join(' ') is calling the
join function of the
Array and passing
' ' (just an empty space), which is also a
String class. It's all classes!
Well, and objects, but let's just skip that word for now. You'll learn
what those are after you make some classes. How do you make classes?
Very similar to how you made the
ROOMS hash, but easier:
class TheThing attr_reader :number def initialize() @number = 0 end def some_function() puts "I got called." end def add_me_up(more) @number += more return @number end end # two different things a = TheThing.new b = TheThing.new a.some_function() b.some_function() puts a.add_me_up(20) puts a.add_me_up(20) puts b.add_me_up(30) puts b.add_me_up(30) puts a.number puts b.number
@ symbol before the
@number variable? That makes it an
instance variable. Every instance of
TheThing that you create will
have its own value for
@number. Instance variables are hidden away
inside the object. We can't get at the name simply by typing
a.number unless we explicitly make that data readable to the
By including the
attr_reader :number line. To make
write-only, we could do
attr_writer :number. And to make it read/write
we could do
attr_accessor :number. Ruby uses the good object-oriented
principle of encapsulating data.
Next, see the
initialize method? That is how you set up a Ruby class
with internal variables. You can set them with the
@ symbol just
like I showed you here. See also how we then use this in
later which lets you add to the
@number you created. Later you can
see how we use this to add to our number and print it.
Classes are very powerful, so you should read everything you can about them and play with them. You actually know how to use them, you just have to try it. In fact, I want to play some guitar right now so I'm not going to give you an exercise to type. You're going to write an exercise using classes.
Here's how we'd do exercise 41 using classes instead of the thing we created:
What You Should See
The output from this version of the game should be exactly the same as the previous version. In fact you'll notice that some of the code is nearly the same. Compare this new version of the game with the last one so you understand the changes that were made. Key things to really get are:
- How you made a
class Gameand put functions inside it.
initializeis a special initialization method that sets up important variables.
- How you added functions to the class by nesting their definitions
- How you nested the contents of the functions under their names.
- The concept of
@and how it's used in
- How a Game was created at the end and then told to
play()and how that got everything started.
- Add some rooms to make sure you know how to work with a class.
- Create a two-class version of this, where one is the
Mapand the other is the
playgoes in the