Method: String#methodize

Defined in:
lib/mark_facets/ruby/string.rb

#methodizeObject

Raises:

  • (NameError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mark_facets/ruby/string.rb', line 26

def methodize
  m = self.dup
  m.strip!
  m.match(/([\?\=\!])$/)
  punc = $1
  m = m.underscore if m.respond_to?(:underscore)
  m.downcase!
  m = m[0..100] if m.length > 100
  m.gsub!(/(\W)/, '_')
  m.gsub!(/_+$/, '')
  m.gsub!(/^[^a-z]+/, '')
  [' ', '_', '?', '!', "="].each do |c|
    m.squeeze!(c)
  end
  raise NameError.new("#{self} cannot be converted to a valid method name!") if m.nil? || m == ''
  return "#{m}#{punc}"
end