
-----------------------------------
wtd
Wed Oct 06, 2004 6:42 pm

[Ruby-tip] Enums... sort of
-----------------------------------
def enum(*args)
   args.each_with_index do |arg, index|
      Object.const_set(arg, index)
   end
end

enum :Sunday, :Monday, :Tuesday, :Wednesday, :Thursday, :Friday, :Saturday

-----------------------------------
Tony
Wed Oct 06, 2004 8:17 pm


-----------------------------------
you might have to explain what enums are and what they are used for

-----------------------------------
wtd
Wed Oct 06, 2004 8:27 pm


-----------------------------------
An enum, at the most basic level is a way of assigning sequential integers to a series of names.

By writing:

enum :Sunday, :Monday, :Tuesday, :Wednesday, :Thursday, :Friday, :Saturday

I'm saying that Sunday is zero, Monday is one, and so on until Saturday is six.  

It can be useful to use this style rather than hard-coded numbers.  Doing so makes your intent more clear, without having to resort to comments.

-----------------------------------
JHanson90
Thu Oct 07, 2004 1:32 pm


-----------------------------------
...  Doing so makes your intent more clear, without having to resort to comments.

Personally, I think that comments can still help someone analyzing your code.  I do not like finding ways to escape comments; no matter how clear the code may be, especially if you're writing in a more complicated language than Ruby.  But do you agree?  Or should I stop using 20% of my programming time writing a novel in my code... :P

-----------------------------------
Tony
Thu Oct 07, 2004 1:59 pm


-----------------------------------
ruby is not a complicated language. It is very preaty and nice to read :) 

though if I know that my code will be read (such as my assignments), I actually enjoy writing short humorous comments next to key points (such as function declarations and if statements)

-----------------------------------
JHanson90
Thu Oct 07, 2004 2:03 pm


-----------------------------------
ruby is not a complicated language. It is very preaty and nice to read :)

Sorry if what I said was unclear; what I was saying that in the case of Ruby comments are not needed as much, but in a more complicated language I would use more comments.

-----------------------------------
wtd
Thu Oct 07, 2004 5:44 pm


-----------------------------------
Ok, a case where "enums" would make Ruby clearer...

We have an array of 7 strings.  Each describes some event that will be occurring on that day of the week.  Obviously there are better, more object-orented ways of doing this, but this is a simple example.  :)

Events are inspired by Gary Larson's "Jurassic Calendars".

events = ["Kill something and eat it", "Kill something and eat it", "Digest", "Kill something and eat it", "Sleep", "Kill something and eat it", "Digest"]

wednesday_event = events[3] # 3 corresponds to the day Wednesday

Or...

def enum(*args)
   args.each_with_index do |arg, index|
      Object.const_set(arg, index)
   end
end

enum :Sunday, :Monday, :Tuesday, :Wednesday, :Thursday, :Friday, :Saturday

events = ["Kill something and eat it", "Kill something and eat it", "Digest", "Kill something and eat it", "Sleep", "Kill something and eat it", "Digest"]

wednesday_event = events[Wednesday]
