Class: String
Instance Method Summary collapse
- #/(o) ⇒ Object
-
#camelcase ⇒ Object
Turn a downcased string and capitalize it so that it can be a class doc_river #=> DocRiver.
-
#classify ⇒ Object
Turn a string from lowercased with a .
-
#constantize(mod = Object) ⇒ Object
Constantize tries to find a declared constant with the name specified in the string.
-
#dasherize ⇒ Object
“FooBar”.dasherize #=> “foo-bar”.
-
#snake_case ⇒ Object
“FooBar”.snake_case #=> “foo_bar”.
Instance Method Details
#/(o) ⇒ Object
44 45 46 |
# File 'lib/core/string.rb', line 44 def /(o) File.join(self, o.to_s) end |
#camelcase ⇒ Object
Turn a downcased string and capitalize it so that it can be a class doc_river #=> DocRiver
5 6 7 |
# File 'lib/core/string.rb', line 5 def camelcase gsub(/(^|_|-)(.)/) { $2.upcase } end |
#classify ⇒ Object
Turn a string from lowercased with a . to a classified classname rice_and_beans #=> “RiceAndBeans” handles subclassed and namespaced classes as well for instance
rice::and::beans #=> Rice::And::Beans
25 26 27 |
# File 'lib/core/string.rb', line 25 def classify self.sub(/.*\./, '').split("::").map {|ele| ele.camelcase }.join("::") end |
#constantize(mod = Object) ⇒ Object
Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.
Examples
"Module".constantize #=> Module
"Class".constantize #=> Class
35 36 37 38 39 40 41 42 |
# File 'lib/core/string.rb', line 35 def constantize(mod=Object) camelcased_word = classify begin mod.module_eval(camelcased_word, __FILE__, __LINE__) rescue NameError nil end end |
#dasherize ⇒ Object
“FooBar”.dasherize #=> “foo-bar”
15 16 17 |
# File 'lib/core/string.rb', line 15 def dasherize gsub(/\B[A-Z]+/, '-\&').downcase end |
#snake_case ⇒ Object
“FooBar”.snake_case #=> “foo_bar”
10 11 12 |
# File 'lib/core/string.rb', line 10 def snake_case gsub(/\B[A-Z]+/, '_\&').downcase end |