Data Structures and Algorithms with Object-Oriented Design Patterns in Ruby
next up previous index

Projects

  1.   Design and implement extensions for the Ruby types Fixnum, String, and Array so that they may be used wherever an instance of the AbstractObject class is used.
  2. Using visitors, devise an implementation for the member? method and the find method of the SearchableContainer class declared in Program gif.
  3. Using an iterator, devise an implementation for the member? method and the find method of the SearchableContainer class declared in Program gif.
  4. Devise a scheme using visitors whereby all of the objects contained in one searchable container can be removed from it and transfered to another container.
  5.   A bag  is a simple container that can hold a collection of objects. Design and implement a concrete class called Bag that extends the SearchableContainer class defined in Program gif. Use the Array class given in Chapter gif to keep track of the contents of the bag.
  6. Repeat Project gif, this time using the LinkedList class given in Chapter gif.
  7. In C# it is common to use an enumerator  as the means to iterate through the objects in a container. In Ruby we can define an enumerator like this:
    class Enumerator < Object
        abstractmethod :reset
        abstractmethod :current
        abstractmethod :moveNext
    end
    The reset method ``resets'' the enumerator to the the first element of the container. The current method returns the current element or nil if there are no more elements to enumerate. The moveNext advances the enumerator to the next element.

    Given an enumerator e from some container c, the contents of c can be printed like this:

    e.reset
    while not e.current.nil?
        print e.current, "\n"
        e.moveNext
    end
    Devise a wrapper class to encapsulate a Ruby iterator and provide the functionality of a C# enumerator.


next up previous index

Bruno Copyright © 2004 by Bruno R. Preiss, P.Eng. All rights reserved.