Jan Humphries

Adventures in Dragon Classes

Jan 24, 2015

The Road from Arrays to Hashes to Classes

So far in our Ruby journey, we've learned how to use objects, then arrays, then hashes, to keep track of increasingly complex but related items. Arrays hold a simple list of items, and hashes hold labelled lists of items. Now we've entered the next chapter: how to collect groups of objects and the methods we want associated with them.

Say you want to keep track of the kinds of dragons you spot on your adventures. You'll want to keep notes about them, and each one will be different. The best way to do so is to create a new Class of objects - Dragon - that will help you save time in your recording.


    class Dragon(color,size,food,abilities,habitat)
      attr_accessor :color, :size, :food , :abilities, :habitat
      def initialize(color,size,food,abilities,habitat)
        @color = color
        @size = size
        @food = food
        @abilities = abilities
        @habitat = habitat
      end
    end

    snuffles = Dragon.new("red","132ft",["sheep","pig","goose","fox"],"Small bursts of fire","Forested hills")
  

At this point, the Class is similar to a Hash, in that you can access the value of data stored with a label, such as snuffles.color #=> returns "red" and snuffles.habitat #=> returns "Forested hills" However, it's much more resuable than a single Hash, because I can create many Dragon objects without re-writing all the Hash syntax junk:


    snuffles = Dragon.new("red","132ft",["sheep","pig","goose","fox"],"Small bursts of fire","Forested hills")
    snuggles = Dragon.new("green","4.3ft",["mice","insects"],"Sense of smell","Undergrowth")
    stumbles = Dragon.new("red-brown","15ft",["birds","snakes","squirrels"],"None","Rocky")
    shuffles = Dragon.new("blue-green","5.5ft",["plants","insects"],"Very quick","Forest canopy")
    # Now we have 4 complete sets of Dragon data with only 4 lines of code!
  

Data *and* Methods

The utility of Classes is even greater when you start adding in Methods you want to use as well. They will be Methods unique to Dragon data, so you only want to include them if and only if you'll use them so.


    class Dragon
      attr_accessor :color, :size, :food , :abilities, :habitat
      def initialize(color,size,food,abilities,habitat)
        @color = color
        @size = size
        @food = food
        @abilities = abilities
        @habitat = habitat
      end

      def feed(offering)
        if @food.any? {|food| food == offering }
          puts "Rumbles with pleasure, you've made a friend!"
        else
          puts "Roars with anger! Run!"
        end
      end
    end

    snuffles = Dragon.new("red","132ft",["sheep","pig","goose","fox"],"Small bursts of fire","Forested hills")
    snuggles = Dragon.new("green","4.3ft",["mice","insects"],"Sense of smell","Undergrowth")
    stumbles = Dragon.new("red-brown","15ft",["birds","snakes","squirrels"],"None","Rocky")
    shuffles = Dragon.new("blue-green","5.5ft",["plants","insects"],"Very quick","Forest canopy")

    snuggles.feed("insects")
    shuffles.feed("squirrels")
  

You can add other methods as well - maybe "pets_head" or "test_ability". Now that we've written our Class, we won't have to re-write any of that code anywhere else in our program for documenting sights on our voyages. We could also make it more general for all types of animals, or more specific for more-defined dragons, depening on what information we want to store and what methods we'd like to call. There are lots of possibilities, just like an adventure!