Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/omdbapi/default.rb

Overview

Reopen the String class to add to_snake_case method.

Instance Method Summary collapse

Instance Method Details

#to_snake_caseString

Convert string to snake case from camel case.

Examples:

"CamelCasedString".to_snake_case # => "camel_cased_string"

Returns:



34
35
36
37
38
39
40
# File 'lib/omdbapi/default.rb', line 34

def to_snake_case
  self.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end