Over a million developers have joined DZone.

Ruby Enumerated List

DZone's Guide to

Ruby Enumerated List

Free Resource
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:

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 }

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}