Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Ruby enumerated list

Basic enumerated list in Ruby. Pass in a list of symbols. A constant is declared for each symbol and is initialized to an incremented integer value.

Use like this:
enum :FIRST, :SECOND, :THIRD

   1  
   2  module Util
   3    # Basic enumerated list.
   4    def enum(*symbols)
   5      # Start out the count at 0.
   6      i = 0
   7      
   8      # Define a constant for each symbol in symbols, assigning it the current value
   9      # of i.
  10      symbols.each { |s| const_set(s, i);i += 1 }
  11    end  
  12  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS