Class: String

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

Instance Method Summary collapse

Instance Method Details

#swap(*substrings) ⇒ Object



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

def swap *substrings
  # there are two arguments and they are both strings
  if substrings.count == 2 && substrings.reject { |substring| substring.class == String }.empty?
    this, that = substrings
    swap_two_substrings this, that
  # all the substrings are arrays AND each array has two elements AND both of those elements are strings
  elsif substrings.reject { |substring_pair| substring_pair.class == Array && substring_pair.count == 2 && substring_pair.reject { |substring| substring.class == String }.empty? }.empty?
    substrings.each do |substring_pair|
      this, that = substring_pair
      replace(swap_two_substrings this, that)
    end
    return self
  else
    raise ArgumentError, 'only strings, or arrays of string pairs are acceptable arguments'
  end
end