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