File: squish.rb

package info (click to toggle)
ruby-powerpack 0.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 492 kB
  • sloc: ruby: 819; makefile: 3
file content (27 lines) | stat: -rw-r--r-- 761 bytes parent folder | download | duplicates (3)
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