[Ruby-tip] Enums... sort of
Author |
Message |
wtd
|
Posted: 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 |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Wed Oct 06, 2004 8:17 pm Post subject: (No subject) |
|
|
you might have to explain what enums are and what they are used for |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
wtd
|
Posted: Wed Oct 06, 2004 8:27 pm Post subject: (No 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. |
|
|
|
|
|
JHanson90
|
Posted: Thu Oct 07, 2004 1:32 pm Post subject: (No 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... |
|
|
|
|
|
Tony
|
Posted: Thu Oct 07, 2004 1:59 pm Post subject: (No subject) |
|
|
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) |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
JHanson90
|
Posted: Thu Oct 07, 2004 2:03 pm Post subject: (No subject) |
|
|
tony wrote: 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
|
Posted: Thu Oct 07, 2004 5:44 pm Post subject: (No 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.
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] |
|
|
|
|
|
|
|
|