Implementing the tape safe enum pattern in Ruby.

G

Ged

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();
}
}
}
 
P

Paul Brannan

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
 
G

Ged

Yes, this is possible in Ruby. See:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top