Hiya!

I'm pretty new to Ruby and I'm looking for an enum-like-type just to make my
code more readeable, but maybe I'm taking a wrong approach (badly influenced
bu others languages I know, no names please :-)) and going against some ruby
pragmatic principles, don't know.
Anyway, if you do know something in the language that might help to
accomplish modeling enums like the example below, in a more clever way, I'll
appreciate the tip.

require 'test/unit'

class DaysOfWeek < String

  @@monday = DaysOfWeek.new("monday")
  @@tuesday = DaysOfWeek.new("tuesday")
  @@wednesday = DaysOfWeek.new("wednesday")
  @@thursday = DaysOfWeek.new("thursday")
  @@friday = DaysOfWeek.new("friday")
  @@saturday = DaysOfWeek.new("saturday")
  @@sunday = DaysOfWeek.new("sunday")

  def DaysOfWeek.Monday
    return @@monday
  end
  def DaysOfWeek.Tuesday
    return @@tuesday
  end
  def DaysOfWeek.Wednesday
    return @@wednesday
  end
  def DaysOfWeek.Thursday
    return @@thursday
  end
  def DaysOfWeek.Friday
    return @@friday
  end
  def DaysOfWeek.Saturday
    return @@saturday
  end
  def DaysOfWeek.Sunday
    return @@sunday
  end

  def DaysOfWeek.to_list
    [ @@monday, @@tuesday, @@wednesday, @@thursday, @@friday, @@saturday,
@@sunday ]
  end

  def DaysOfWeek.from_s(content)
    to_list().find { |item| item == content }
  end

  private
    def initialize(str)
      super(str)
    end
end

class TempTest < Test::Unit::TestCase
  def test_enum
    assert_equal "monday", DaysOfWeek.Monday
    assert_equal "tuesday", DaysOfWeek.Tuesday
    assert_equal DaysOfWeek.Monday, DaysOfWeek.from_s("monday")
  end
end


Cheers,
--
hammett
http://www.apache.org/~hammett/
http://jroller.com/page/hammett