Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/basic.rb
Overview
open String and provide some helpful methods
Instance Method Summary collapse
-
#camelcase ⇒ String
“CamelCase” fron “snake_case”.
-
#sanitize ⇒ String
With remove non-word characters and adding ?n prfix when it starts from number.
-
#snakecase ⇒ String
“snake_case” from “CamelCase”.
- #underscore ⇒ Object
Instance Method Details
#camelcase ⇒ String
Returns “CamelCase” fron “snake_case”.
24 25 26 |
# File 'lib/basic.rb', line 24 def camelcase split(/_/).map(&:capitalize).join end |
#sanitize ⇒ String
Returns with remove non-word characters and adding ?n prfix when it starts from number.
6 7 8 9 10 |
# File 'lib/basic.rb', line 6 def sanitize gsub(/[^\w\s]/, '') .gsub(/\s{1,}/, ' ') .then{ it.start_with?(/\d/) ? "n#{it}" : it } end |
#snakecase ⇒ String
Returns “snake_case” from “CamelCase”.
17 18 19 20 21 |
# File 'lib/basic.rb', line 17 def snakecase gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase end |
#underscore ⇒ Object
12 13 14 |
# File 'lib/basic.rb', line 12 def underscore gsub(/\s{1,}/, ?_) end |