Class: String

Inherits:
Object show all
Defined in:
lib/nanite/util.rb

Instance Method Summary collapse

Instance Method Details

#snake_caseString

Convert to snake case.

"FooBar".snake_case           #=> "foo_bar"
"HeadlineCNNNews".snake_case  #=> "headline_cnn_news"
"CNN".snake_case              #=> "cnn"

Returns:

  • (String)

    Receiver converted to snake case.



12
13
14
15
16
# File 'lib/nanite/util.rb', line 12

def snake_case
  return self.downcase if self =~ /^[A-Z]+$/
  self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
  return $+.downcase
end

#to_const_pathString

Convert a constant name to a path, assuming a conventional structure.

"FooBar::Baz".to_const_path # => "foo_bar/baz"

Returns:

  • (String)

    Path to the file containing the constant named by receiver (constantized string), assuming a conventional structure.



27
28
29
# File 'lib/nanite/util.rb', line 27

def to_const_path
  snake_case.gsub(/::/, "/")
end