Class: String

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

Overview

Some simple string extensions to make things easier.

Instance Method Summary collapse

Instance Method Details

#compress_linesString

Replace sequences of whitespace (including newlines) with either a single space or remove them entirely (according to param spaced)

<<QUERY.compress_lines
  SELECT name
  FROM users
QUERY => "SELECT name FROM users"

Returns:

  • (String)

    Receiver with whitespace (including newlines) replaced



14
15
16
# File 'lib/montage/core_ext.rb', line 14

def compress_lines
  split($/).map { |line| line.strip }.join(' ')
end

#unindentString

Removes leading whitespace from each line, such as might be added when using a HEREDOC string.

Returns:

  • (String)

    Receiver with leading whitespace removed.



23
24
25
# File 'lib/montage/core_ext.rb', line 23

def unindent
  (other = dup) and other.unindent! and other
end

#unindent!String

Bang version of #unindent.

Returns:

  • (String)

    Receiver with leading whitespace removed.



31
32
33
# File 'lib/montage/core_ext.rb', line 31

def unindent!
  gsub!(/^[ \t]{#{minimum_leading_whitespace}}/, '')
end