Module: StringPlus
- Defined in:
- lib/string_plus.rb,
lib/string_plus/version.rb
Constant Summary collapse
- VERSION =
"0.5.3"
Class Method Summary collapse
- .camelcase(str = self, capitalize_first_char = true) ⇒ Object
- .constantize(str = self) ⇒ Object
- .lcamelcase(str = self) ⇒ Object
- .underscore(str = self) ⇒ Object (also: snakecase)
Class Method Details
.camelcase(str = self, capitalize_first_char = true) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/string_plus.rb', line 5 def camelcase(str=self, capitalize_first_char=true) res = "" flag = capitalize_first_char str.each_char {|w| (flag = true ; next) if w == "_" || w == " " res << if flag flag = false w.upcase else w end } res end |
.constantize(str = self) ⇒ Object
35 36 37 38 |
# File 'lib/string_plus.rb', line 35 def constantize(str=self) c = str.split("-").map {|segment| camelcase(segment)}.join("::") Object.send(:const_get, c) end |
.lcamelcase(str = self) ⇒ Object
20 21 22 |
# File 'lib/string_plus.rb', line 20 def lcamelcase(str=self) camelcase(str, false) end |
.underscore(str = self) ⇒ Object Also known as: snakecase
24 25 26 27 28 29 30 31 32 |
# File 'lib/string_plus.rb', line 24 def underscore(str=self) return str.downcase if str.each_char.all? { |c| c == c.upcase && c =~ /[a-zA-Z0-9]/} res = "" [nil, *str.each_char].each_cons(2) do |last, current| res << "_" if last !=nil && current == current.upcase && !("0".."9").include?(current) res << current.downcase unless current == " " end res end |