Implementing the tape safe enum pattern in Ruby.

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Implementing the tape safe enum pattern in Ruby.

    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 Guest

  2. Similar Questions and Discussions

    1. behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?
      String#gsub(pattern) {|m| ... } It really would be nice to get match data in 'm', but this would surely break _a lot_ of scripts. How about...
    2. POLS ANT file pattern in Ruby
      This is a multi-part message in MIME format. ------=_NextPart_000_094E_01C39585.7A570C60 Content-Type: text/plain; charset="iso-8859-1"...
    3. POLS ANT file pattern in Ruby<Pine.LNX.4.44.0310181631510.7967-100000@ool-4355dfae.dyn.optonline.net>
      Hi -- On Sat, 18 Oct 2003, Robert Dawson wrote: Just eyeballing it, I would expect this assertion to be true. I guess that means I would...
    4. Does Ruby 1.8.0 improve in file I/O speed and pattern match speed?
      Hi, rubyists, I'm using ruby 1.6.8 (2002-12-24) and find file I/O too slow. Is ruby 1.8.0 faster? Some one in the list said speed is quite...
    5. $SAFE = 5 and Safe Ruby Misleading?
      Hey folks. With all this talk of duck typing and such, I got to thinking about some of my code that I *thought* executed untrusted code...
  3. #2

    Default Re: Implementing the tape safe enum pattern in Ruby.

    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:
    [url]http://cvs.sf.net/cgi-bin/viewcvs.cgi/rubycollections/rubycollections/rbc/enum.rb?rev=1.7[/url]
    [url]http://cvs.sf.net/cgi-bin/viewcvs.cgi/excruby/excruby/excruby/enum_wrapper.hpp?rev=1.2[/url]
    [url]http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/79041[/url]
    > 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 Guest

  4. #3

    Default Re: Implementing the tape safe enum pattern in Ruby.

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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139