You are here: > S/W Design and Architecture > Class Exercise: Singleton

Class Exercise: Singleton

Note: This is a class activity, nothing needs to be delivered.

  1. Create a folder called singleton. Inside this folder, create two files called: tigger.rb and tc_tigger.rb.

  2. You are given the following Ruby class that models the famous Tigger character from Winnie The Pooh:

    # File: tigger.rb
    class Tigger
      def to_s
        return "I'm the only one!"
      def roar

    Convert this class into a singleton using whatever technique you find fit. Place the resulting class in the tigger.rb source file.

  3. Check your solution using the following test case (place the test in the source file tc_tigger.rb):

    # File: tc_tigger.rb
    require 'test/unit'
    require 'tigger'
    class TiggerTest < Test::Unit::TestCase
      def test_tigger
        t = Tigger.instance
        assert_same(t, Tigger.instance)
        assert_raise(NoMethodError) do       # "new" method should be private!

        assert_equal("I'm the only one!", t.to_s)
        assert_equal('Grrr!', t.roar)