Implementing the tape safe enum pattern in Ruby.

Discussion in 'Ruby' started by Ged, Sep 15, 2003.

  1. Ged

    Ged Guest

    I rather like the type safe enum pattern in Java, and I was wonding
    how I might implement it in Ruby. Here is an example (based on the
    earlier OO challange thread).

    The big advantage is that it allows me to dynamically specify a
    domain. Iteration and lookup comes free, all I have to do is declare
    each variation. I find constant use for it, avoiding the need for
    reflection.

    Is an equivalent possible in Ruby, or is it irrelevant because Ruby is
    not strongly typed?

    abstract class TaxGroup {

    static private TaxGroup lastDeclaredGroup;

    static public Iterator iterator() {
    return new TaxGroupIterator();
    }

    private String name;

    private TaxGroup() {}

    private TaxGroup nextGroup;

    private TaxGroup(String groupName) {
    name = groupName;
    nextGroup = lastDeclaredGroup;
    lastDeclaredGroup = this;
    }
    abstract public long calculate(long salary);

    final public String toString() {
    return "Tax_" + name;
    }
    static final public TaxGroup SOLDIER = new TaxGroup("Soldier") {
    public long calculate(long salary) {
    return salary / 10;
    }
    };
    static final public TaxGroup PROFESSOR = new TaxGroup("Professor") {
    public long calculate(long salary) {
    return (salary/15)+100;
    }
    };
    //many of them

    private static class TaxGroupIterator implements Iterator {
    private TaxGroup currentTaxGroup;

    public TaxGroupIterator() {
    currentTaxGroup = lastDeclaredGroup;
    }
    public Object next() {
    TaxGroup returnGroup = currentTaxGroup;
    currentTaxGroup = returnGroup.nextGroup;
    return returnGroup;
    }
    public boolean hasNext() {
    return currentTaxGroup != null;
    }
    public void remove() {
    throw new UnsupportedOperationException();
    }
    }
    }
     
    Ged, Sep 15, 2003
    #1
    1. Advertising

  2. Ged

    Paul Brannan Guest

    On Tue, Sep 16, 2003 at 01:47:46AM +0900, Ged wrote:
    > I rather like the type safe enum pattern in Java, and I was wonding
    > how I might implement it in Ruby. Here is an example (based on the
    > earlier OO challange thread).
    >
    > The big advantage is that it allows me to dynamically specify a
    > domain. Iteration and lookup comes free, all I have to do is declare
    > each variation. I find constant use for it, avoiding the need for
    > reflection.


    Yes, this is possible in Ruby. See:
    http://cvs.sf.net/cgi-bin/viewcvs.cgi/rubycollections/rubycollections/rbc/enum.rb?rev=1.7
    http://cvs.sf.net/cgi-bin/viewcvs.cgi/excruby/excruby/excruby/enum_wrapper.hpp?rev=1.2
    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/79041

    > Is an equivalent possible in Ruby, or is it irrelevant because Ruby is
    > not strongly typed?


    Ruby is strongly typed. It is not statically typed. See
    [ruby-talk:64625].

    Paul
     
    Paul Brannan, Sep 15, 2003
    #2
    1. Advertising

  3. Ged

    Ged Guest

    > Yes, this is possible in Ruby. See:
    > http://cvs.sf.net/cgi-bin/viewcvs.cgi/rubycollections/rubycollections/rbc/enum.rb?rev=1.7
    > http://cvs.sf.net/cgi-bin/viewcvs.cgi/excruby/excruby/excruby/enum_wrapper.hpp?rev=1.2
    > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/79041
    >

    Paul,

    Thanks for those. The C++ example is useful too.

    > > Is an equivalent possible in Ruby, or is it irrelevant because Ruby is
    > > not strongly typed?

    >
    > Ruby is strongly typed. It is not statically typed. See
    > [ruby-talk:64625].
    >
    > Paul


    Thanks especially for this distinction. It's helping to understand things better.
     
    Ged, Sep 16, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan
    Replies:
    2
    Views:
    2,118
  2. Charles P Lamb

    SCSI, tape, and scanner I/O?

    Charles P Lamb, Jul 26, 2005, in forum: Java
    Replies:
    0
    Views:
    450
    Charles P Lamb
    Jul 26, 2005
  3. Stylus Studio
    Replies:
    0
    Views:
    381
    Stylus Studio
    Sep 7, 2005
  4. William
    Replies:
    1
    Views:
    427
    Kevin Goodsell
    Sep 9, 2003
  5. Nix!
    Replies:
    9
    Views:
    374
    Ted Zlatanov
    Mar 4, 2010
Loading...

Share This Page