Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/constant_cache/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#constant_nameObject

A method to create a constant name from the existing string. This method condenses multiple non-word characters into a single underscore:

'abc'.constant_name                => 'ABC'
'Restaurants & Bars'.constant_name => 'RESTAURANTS_BARS'


10
11
12
13
# File 'lib/constant_cache/core_ext.rb', line 10

def constant_name
  value = self.strip.gsub(/\s+/, '_').gsub(/[^\w_]/, '').gsub(/_{2,}/, '_').upcase
  (value == '') ? nil : value
end