Module: CoreExtensions::String
- Included in:
- String
- Defined in:
- lib/extlib_lite/core_extensions/string.rb
Instance Method Summary collapse
-
#from_query ⇒ Hash
Convert to hash from a query string.
-
#snake_case ⇒ String
Convert to snake case.
-
#to_const_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
Instance Method Details
#from_query ⇒ Hash
Convert to hash from a query string.
“text_message%5Bto%5D=61447100308&text_message%5Bfrom%5D=61447100547&text_message%5Bmsg%5D=Edin%20knif%20lie%20km&text_message%5Bdate%5D=2010-05-13%2023%3A59%3A58”.from_query #=> {
"text_message"=>{
"to"=>"61447100308",
"from"=>"61447100547",
"msg"=>"Edin knif lie km",
"date"=>"2010-05-13 23:59:58"
}
}
19 20 21 22 23 |
# File 'lib/extlib_lite/core_extensions/string.rb', line 19 def from_query uri = Addressable::URI.new uri.query = self uri.query_values end |
#snake_case ⇒ String
Convert to snake case.
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
48 49 50 51 52 53 |
# File 'lib/extlib_lite/core_extensions/string.rb', line 48 def snake_case return downcase if match(/\A[A-Z]+\z/) gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z])([A-Z])/, '\1_\2'). downcase end |
#to_const_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
"FooBar::Baz".to_const_path # => "foo_bar/baz"
34 35 36 |
# File 'lib/extlib_lite/core_extensions/string.rb', line 34 def to_const_path snake_case.gsub(/::/, "/") end |