Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/casual_support/string/drop.rb,
lib/casual_support/string/from_to.rb,
lib/casual_support/string/first_last.rb,
lib/casual_support/string/before_after.rb

Instance Method Summary collapse

Instance Method Details

#after(delim) ⇒ String

Searches for the first occurrence of a delimiter, and returns the portion of the string after that. If the delimiter is not found, returns the original string.

Semantically equivalent to:

split(delim, 2).last

Parameters:

  • delim (String)

    delimiter to search for

Returns:

  • (String)

    portion of the string after the first delim



40
41
42
43
# File 'lib/casual_support/string/before_after.rb', line 40

def after(delim)
  i = self.index(delim)
  i ? self.drop(i + delim.length) : self
end

#after_last(delim) ⇒ String

Searches for the last occurrence of a delimiter, and returns the portion of the string after that. If the delimiter is not found, returns the original string.

Semantically equivalent to:

split(delim).last

Parameters:

  • delim (String)

    delimiter to search for

Returns:

  • (String)

    portion of the string after the last delim



54
55
56
57
# File 'lib/casual_support/string/before_after.rb', line 54

def after_last(delim)
  i = self.rindex(delim)
  i ? self.drop(i + delim.length) : self
end

#before(delim) ⇒ String

Searches for the first occurrence of a delimiter, and returns the portion of the string before that. If the delimiter is not found, returns the original string.

Semantically equivalent to:

split(delim).first

Parameters:

  • delim (String)

    delimiter to search for

Returns:

  • (String)

    portion of the string before the first delim



12
13
14
15
# File 'lib/casual_support/string/before_after.rb', line 12

def before(delim)
  i = self.index(delim)
  i ? self.first(i) : self
end

#before_last(delim) ⇒ String

Searches for the last occurrence of a delimiter, and returns the portion of the string before that. If the delimiter is not found, returns the original string.

Semantically equivalent to:

split(delim)[0..-2].join(delim)

Parameters:

  • delim (String)

    delimiter to search for

Returns:

  • (String)

    portion of the string before the last delim



26
27
28
29
# File 'lib/casual_support/string/before_after.rb', line 26

def before_last(delim)
  i = self.rindex(delim)
  i ? self.first(i) : self
end

#drop(n) ⇒ String

Drops characters from the beginning of a string, and returns the remainder. If the number of characters to drop is greater than the length of the string, an empty string is returned.

Parameters:

  • n (Integer)

    number of characters to drop

Returns:

  • (String)

    string of remaining characters



9
10
11
# File 'lib/casual_support/string/drop.rb', line 9

def drop(n)
  self[n.constrain(0, self.length), self.length]
end

#first(limit = 1) ⇒ Object

This replaces Active Support’s String#first, but it returns an empty string when given a negative argument. It is ~75% faster.



5
6
7
# File 'lib/casual_support/string/first_last.rb', line 5

def first(limit = 1)
  limit <= 0 ? '' : self[0, limit]
end

#from(position) ⇒ Object

This replaces Active Support’s String#from. It is ~40% faster.



4
5
6
7
# File 'lib/casual_support/string/from_to.rb', line 4

def from(position)
  position += self.length if position < 0
  self[position, length]
end

#last(limit = 1) ⇒ Object

This replaces Active Support’s String#last, but it returns an empty string when given a negative argument. It is ~75% faster.



11
12
13
# File 'lib/casual_support/string/first_last.rb', line 11

def last(limit = 1)
  limit <= 0 ? '' : self[length - limit, length]
end

#to(position) ⇒ Object

This replaces Active Support’s String#to. It is ~40% faster.



10
11
12
13
# File 'lib/casual_support/string/from_to.rb', line 10

def to(position)
  position += self.length if position < 0
  self[0, position + 1]
end