1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#!/usr/bin/env ruby
require "rubygems"
require "rbench"
class String
##
# @return <String> The string converted to snake case.
#
# @example
# "FooBar".snake_case #=> "foo_bar"
# @example
# "HeadlineCNNNews".snake_case #=> "headline_cnn_news"
# @example
# "CNN".snake_case #=> "cnn"
def snake_case
return self.downcase if self =~ /^[A-Z]+$/
self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
return $+.downcase
end
##
# @return <String>
# The path that is associated with the constantized string, assuming a
# conventional structure.
#
# @example
# "FooBar::Baz".to_const_path # => "foo_bar/baz"
def to_const_path
snake_case.gsub(/::/, "/")
end
# The reverse of +camelize+. Makes an underscored form from the expression in the string.
#
# Changes '::' to '/' to convert namespaces to paths.
#
# @example
# "ActiveRecord".underscore #=> "active_record"
# "ActiveRecord::Errors".underscore #=> active_record/errors
#
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end # class String
RBench.run(10_000) do
report "String#underscore" do
"CamelCaseString".underscore
"SomeABitLongerCamel::CaseString".underscore
end
report "String#to_const_path" do
"CamelCaseString".to_const_path
"SomeABitLongerCamel::CaseString".to_const_path
end
report "String#snake_case" do
"CamelCaseString".snake_case
"SomeABitLongerCamelCaseString".snake_case
end
end
|