Sha256: c9a281f4f5ea4f37179ef6a9d2656fe7f24255f6a651b1d4aaede85b36556b6d
Contents?: true
Size: 1.57 KB
Versions: 2
Compression:
Stored size: 1.57 KB
Contents
#!/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
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rango-0.0.6 | vendor/extlib/benchmarks/to_const_path.rb |
rango-0.1.pre | vendor/extlib/benchmarks/to_const_path.rb |