Module: RSplit

Defined in:
lib/rsplit.rb,
lib/rsplit/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.rsplit(str, sep = nil, limit = 0) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rsplit.rb', line 4

def rsplit(str, sep = nil, limit = 0)
  valid_encoding?(str)
  sep = $; if sep.nil?
  case sep
  when NilClass
    # do nothing
  when String
    valid_encoding?(sep)
    sep = sep.reverse
  else
    raise TypeError, "wrong argument type #{sep.class} (expected String)"
  end

  str.reverse.split(sep, limit).map(&:reverse).reverse
end

.valid_encoding?(string) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


24
25
26
# File 'lib/rsplit.rb', line 24

def self.valid_encoding?(string)
  raise ArgumentError, "invalid byte sequence in #{string.encoding.name}" unless string.valid_encoding?
end