Module: Corefines::String::SnakeCase

Defined in:
lib/corefines/string.rb

Instance Method Summary collapse

Instance Method Details

#snake_caseString

Returns a copy of the str converted to snake_case.

Examples:

"snakeCase".snake_case       # => "snake_case"
"SNAkeCASe".snake_case       # => "sn_ake_ca_se"
"Snake2Case".snake_case      # => "snake2_case"
"snake2case".snake_case      # => "snake2case"
"snake-Ca-se".snake_case     # => "snake_ca_se"
"snake  ca se".snake_case    # => "snake__ca_se"
"__snake-case__".snake_case  # => "__snake_case__"

366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/corefines/string.rb', line 366

module SnakeCase
  refine ::String do
    def snake_case
      self.dup.tap do |s|
        s.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
        s.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
        s.tr!('-', '_')
        s.gsub!(/\s/, '_')
        s.downcase!
      end
    end

    alias_method :snakecase, :snake_case
  end
end

#snakecaseString

Alias for #snake_case.


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/corefines/string.rb', line 366

module SnakeCase
  refine ::String do
    def snake_case
      self.dup.tap do |s|
        s.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
        s.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
        s.tr!('-', '_')
        s.gsub!(/\s/, '_')
        s.downcase!
      end
    end

    alias_method :snakecase, :snake_case
  end
end