Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/basic.rb

Overview

open String and provide some helpful methods

Instance Method Summary collapse

Instance Method Details

#camelcaseString

Returns “CamelCase” fron “snake_case”.

Returns:

  • (String)

    “CamelCase” fron “snake_case”



24
25
26
# File 'lib/basic.rb', line 24

def camelcase
  split(/_/).map(&:capitalize).join
end

#sanitizeString

Returns with remove non-word characters and adding ?n prfix when it starts from number.

Returns:

  • (String)

    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

#snakecaseString

Returns “snake_case” from “CamelCase”.

Returns:

  • (String)

    “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

#underscoreObject



12
13
14
# File 'lib/basic.rb', line 12

def underscore 
  gsub(/\s{1,}/, ?_)
end