Ask a Question related to Ruby, Design and Development.
-
Ged #1
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
-
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... -
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"... -
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... -
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... -
$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... -
Paul Brannan #2
Re: Implementing the tape safe enum pattern in Ruby.
On Tue, Sep 16, 2003 at 01:47:46AM +0900, Ged wrote:
Yes, this is possible in Ruby. See:> 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.
[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]
Ruby is strongly typed. It is not statically typed. See> Is an equivalent possible in Ruby, or is it irrelevant because Ruby is
> not strongly typed?
[ruby-talk:64625].
Paul
Paul Brannan Guest
-
Ged #3
Re: Implementing the tape safe enum pattern in Ruby.
> Yes, this is possible in Ruby. See:
Paul,> [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]
>
Thanks for those. The C++ example is useful too.
Thanks especially for this distinction. It's helping to understand things better.>> > 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
Ged Guest



Reply With Quote

