Class: String

Inherits:
Object show all
Defined in:
lib/dohutil/core_ext/string.rb

Instance Method Summary collapse

Instance Method Details

#after(substr) ⇒ Object



18
19
20
21
22
# File 'lib/dohutil/core_ext/string.rb', line 18

def after(substr)
  loc = index(substr)
  return nil if loc.nil?
  mid(loc + substr.size)
end

#before(substr) ⇒ Object



30
31
32
33
34
# File 'lib/dohutil/core_ext/string.rb', line 30

def before(substr)
  loc = index(substr)
  return nil if loc.nil?
  firstn(loc)
end

#firstn(limit) ⇒ Object



2
3
4
5
6
# File 'lib/dohutil/core_ext/string.rb', line 2

def firstn(limit)
  return nil if limit < 0
  return '' if limit == 0
  slice(Range.new(0, limit - 1))
end

#lastn(limit) ⇒ Object



8
9
10
11
12
# File 'lib/dohutil/core_ext/string.rb', line 8

def lastn(limit)
  return nil if limit < 0
  return '' if limit == 0
  slice(Range.new(-limit, -1)) || self
end

#mid(first, count = nil) ⇒ Object



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

def mid(first, count = nil)
  slice(first, count || (size - first))
end

#rafter(substr) ⇒ Object



24
25
26
27
28
# File 'lib/dohutil/core_ext/string.rb', line 24

def rafter(substr)
  loc = rindex(substr)
  return nil if loc.nil?
  mid(loc + substr.size)
end

#rbefore(substr) ⇒ Object



36
37
38
39
40
# File 'lib/dohutil/core_ext/string.rb', line 36

def rbefore(substr)
  loc = rindex(substr)
  return nil if loc.nil?
  firstn(loc)
end