Computer Science Canada

[Ruby-tip] Enums... sort of

Author:  wtd [ Wed Oct 06, 2004 6:42 pm ]
Post subject:  [Ruby-tip] Enums... sort of

code:
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

Author:  Tony [ Wed Oct 06, 2004 8:17 pm ]
Post subject: 

you might have to explain what enums are and what they are used for

Author:  wtd [ Wed Oct 06, 2004 8:27 pm ]
Post subject: 

An enum, at the most basic level is a way of assigning sequential integers to a series of names.

By writing:

code:
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.

Author:  JHanson90 [ Thu Oct 07, 2004 1:32 pm ]
Post subject: 

wtd wrote:
... 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... Razz

Author:  Tony [ Thu Oct 07, 2004 1:59 pm ]
Post subject: 

ruby is not a complicated language. It is very preaty and nice to read Smile

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)

Author:  JHanson90 [ Thu Oct 07, 2004 2:03 pm ]
Post subject: 

tony wrote:
ruby is not a complicated language. It is very preaty and nice to read Smile


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.

Author:  wtd [ Thu Oct 07, 2004 5:44 pm ]
Post subject: 

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. Smile

Events are inspired by Gary Larson's "Jurassic Calendars".

code:
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...

code:
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]


: