Module: Stringub::Commons

Included in:
String
Defined in:
lib/stringub-commons.rb,
lib/stringub-commons/version.rb

Constant Summary collapse

WORD_PATTERN =

Regexp pattern used to define words.

/\w[\w\'\-]*/
ANY_SPACE_PATTERN =

Regexp pattern used to match any kind of space, ex: ‘ ’, n, r, t

/\s+/
UNIQUE_SPACE =
" "
VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#unique_spacesObject

Removes a sequence of any kind of space characters per a unique whitespace.

# Ex:
"       \n    abc    ".unique_spaces
# >> " abc "

"\o/   \n\r\t     ".unique_spaces
# >> "\o/ "

Tip:

If do you need remove trailing whitespaces. chain the String#strip method:

"       \n    abc    ".unique_spaces.strip
# >> "abc"


38
39
40
# File 'lib/stringub-commons.rb', line 38

def unique_spaces
  self.gsub(ANY_SPACE_PATTERN, UNIQUE_SPACE)
end

#unique_spaces!Object

Removes a sequence of any kind of space characters per a unique whitespace. Returns nil if str was not altered.



44
45
46
# File 'lib/stringub-commons.rb', line 44

def unique_spaces!
  self.gsub!(ANY_SPACE_PATTERN, UNIQUE_SPACE)
end

#wordsObject

Split strings into an array of words.

# Ex:
"Ruby on Rails".words
# >> ["Ruby", "on", "Rails"]

"Serradura's house".words
# >> ["Serradura's", "house"]

"Module and sub-module".words
# >> ["Module", "and",  "aub-module"]


23
24
25
# File 'lib/stringub-commons.rb', line 23

def words
  self.scan(WORD_PATTERN)
end