Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Ruby-tip] Enums... sort of
Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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
Sponsor
sponsor
Tony




PostPosted: 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




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




PostPosted: 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... Razz
Tony




PostPosted: 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 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)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
JHanson90




PostPosted: 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 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.
wtd




PostPosted: 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. 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]
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: