Module: Stringub::Commons

Included in:
String
Defined in:
lib/stringub-commons/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.3"

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   \n\n\r\t def \n\r  ghi \n".unique_spaces
>> " abc def ghi "

Tip:

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

"       \n    abc   \n\n\r\t def \n\r  ghi \n".unique_spaces.strip
>> "abc def ghi"


47
48
49
# File 'lib/stringub-commons/commons.rb', line 47

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.



53
54
55
# File 'lib/stringub-commons/commons.rb', line 53

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"]

If you question yourself: Whats the difference between the standard method String#split and words method?

"a, b, c, d-e,@ f'g.**".split
>> ["a,", "b,", "c,", "d-e,@", "f'g.**"]

"a, b, c, d-e,@ f'g.**".words
>> ["a", "b", "c", "d-e", "f'g"]

The answer is: the words method just take words!



33
34
35
# File 'lib/stringub-commons/commons.rb', line 33

def words
  self.scan(WORD_PATTERN)
end