Over a million developers have joined DZone.

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


module Util
  # Basic enumerated list.
  def enum(*symbols)
    # Start out the count at 0.
    i = 0
    
    # Define a constant for each symbol in symbols, assigning it the current value
    # of i.
    symbols.each { |s| const_set(s, i);i += 1 }
  end  
end
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}