1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
unless String.method_defined? :squish
class String
# Strips leading and trailing whitespace and squashes internal whitespace.
#
# @return [String] a new string with no leading and trailing
# whitespace and no consecutive whitespace characters inside it
#
# @example
# ' Peter Parker'.squish #=> 'Peter Parker'
def squish
dup.squish!
end
# Strips leading and trailing whitespace and squashes internal whitespace.
#
# @return [String] the string with no leading and trailing whitespace and no
# consecutive whitespace characters inside it
#
# @example
# ' Peter Parker'.squish #=> 'Peter Parker'
def squish!
strip!
gsub!(/\s+/, ' ')
self
end
end
end
|