Check if a constant is already defined Check if a constant is already defined ruby ruby

Check if a constant is already defined


CONST = 2 unless defined? CONST

See here for more about awesome defined? operator.

P.S. And in the future I guess you'll want var ||= 1 instead of var = var||1.


const_defined? API

pry> User.const_defined?("PER_PAGE")=> truepry> User.const_defined?("PER_PAGE123")=> false


CONST ||= :default_value

the above works for me on ruby 1.9.3 but fails on 1.8... well 1.8 is ancient now.