Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
124 lines (96 sloc) 4.29 KB

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 keyword. Using 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 hashes with functions.

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 class. The stuff.join(' ') is calling the join function of the Array and passing ' ' (just an empty space), which is also a class, 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

  def some_function()
    puts "I got called."

  def add_me_up(more)
    @number += more
    return @number

# two different things
a =
b =


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

See the @ 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 outside world.

By including the attr_reader :number line. To make @number 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 add_me_up() 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:

  1. How you made a class Game and put functions inside it.
  2. How initialize is a special initialization method that sets up important variables.
  3. How you added functions to the class by nesting their definitions under the class keyword.
  4. How you nested the contents of the functions under their names.
  5. The concept of @ and how it's used in initialize, play, and death.
  6. How a Game was created at the end and then told to play() and how that got everything started.

Extra Credit

  1. Add some rooms to make sure you know how to work with a class.
  2. Create a two-class version of this, where one is the Map and the other is the Engine. Hint: play goes in the Engine.
Jump to Line
Something went wrong with that request. Please try again.