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

Simple enum creation (See related posts)

// simple Enum creation

   1  
   2  module Kernel
   3    # simple (sequential) enumerated values
   4    def enum(*syms)
   5      syms.each { |s| const_set(s, s.to_s) }
   6      const_set(:DEFAULT, syms.first) unless syms.nil?
   7    end
   8  end
   9  
  10  //and the usage
  11  
  12  require 'kernel'
  13  
  14  
  15  
  16  module Constants
  17  
  18    module Gradient
  19  
  20      enum :DOWNSLOPE, :LEVEL, :UPSLOPE
  21  
  22    end
  23  
  24    
  25  
  26    module TreeCover
  27  
  28      enum :GOOD, :BAD, :OK
  29  
  30    end
  31  
  32    
  33  
  34    module TrafficDensity
  35  
  36      enum :LOW, :MEDIUM, :HIGH
  37  
  38    end
  39  
  40  end

Comments on this post

sbeckeriv posts on Mar 11, 2008 at 13:56
   1  
   2  module Kernel
   3    # simple (sequential) enumerated values
   4    def enum(*syms)
   5      if syms
   6        syms.each { |s| const_set(s, s.to_s) }
   7        const_set(:DEFAULT, syms.first) 
   8      end
   9    end
  10  end


You need to create an account or log in to post comments to this site.


Click here to browse all 5562 code snippets

Related Posts