Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/facets/core/string/starts_with.rb,
lib/facets/core/string/blank.rb,
lib/facets/core/string/indent.rb,
lib/masterview/core_ext/string.rb

Overview

– Credit goes to Lucas Carlson and Blaine Cook. ++

Instance Method Summary collapse

Instance Method Details

#blank?Boolean Also known as: whitespace?

Is this string just whitespace?

"abc".blank?  #=> false
"   ".blank?  #=> true


9
10
11
# File 'lib/facets/core/string/blank.rb', line 9

def blank?
  self !~ /\S/
end

#downcase_first_letterObject

returns a string with the first letter downcased, the rest is unchanged



4
5
6
# File 'lib/masterview/core_ext/string.rb', line 4

def downcase_first_letter
  self[0,1].downcase + self[1..-1]
end

#downcase_first_letter!Object

downcases the first letter in place



9
10
11
12
13
# File 'lib/masterview/core_ext/string.rb', line 9

def downcase_first_letter!
  lc_first_letter = self[0,1].downcase
  self[0] = lc_first_letter
  self
end

#ends_with?(str) ⇒ Boolean



11
12
13
# File 'lib/facets/core/string/starts_with.rb', line 11

def ends_with?(str)
  self.rindex( str ) == self.length - str.length
end

#indent(n) ⇒ Object

Indent left or right by n spaces. (This used to be called #tab and aliased as #indent.)



25
26
27
28
29
30
31
# File 'lib/facets/core/string/indent.rb', line 25

def indent(n)
  if n >= 0
    gsub(/^/, ' ' * n)
  else
    gsub(/^ {0,#{-n}}/, "")
  end
end

#starts_with?(str) ⇒ Boolean



7
8
9
# File 'lib/facets/core/string/starts_with.rb', line 7

def starts_with?(str)
  self.index( str ) == 0
end

#tab(n) ⇒ Object

Aligns each line n spaces. (This used to be #taballto.)



7
8
9
# File 'lib/facets/core/string/indent.rb', line 7

def tab(n)
  gsub(/^ */, ' ' * n)
end

#tabto(n) ⇒ Object

Preserves relative tabbing. The first non-empty line ends up with n spaces before nonspace.



14
15
16
17
18
19
20
# File 'lib/facets/core/string/indent.rb', line 14

def tabto(n)
  if self =~ /^( *)\S/
    indent(n - $1.length)
  else
    self
  end
end