Class: StringThing

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

Instance Method Summary collapse

Constructor Details

#initialize(pattern = ['split-halves', 'reverse', 'shift', 'swap-case', 'rotate']) ⇒ StringThing

Returns a new instance of StringThing.



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

def initialize(pattern = ['split-halves', 'reverse', 'shift', 'swap-case', 'rotate'])
    patternOpMap = {
        'split-halves' => '0',
        'reverse' => '1',
        'shift' => '2',
        'swap-case' => '3',
        'rotate' => '4',
    }

    patternString = pattern.map { |op| patternOpMap[op] }.join('')

		errorChecks.each do |error|
        if patternString.include? error['match']
            raise "Error: #{error['description']}"
        end
    end

    @opFunctionMap = opFunctionMap
		@pattern = pattern;
end

Instance Method Details

#decode(text) ⇒ Object



27
28
29
# File 'lib/string_thing.rb', line 27

def decode(text)
    @pattern.reduce(text) { |newText, op| @opFunctionMap.key?(op) ? opFunctionMap[op].call(newText, true) : newText }
end

#encode(text) ⇒ Object



23
24
25
# File 'lib/string_thing.rb', line 23

def encode(text)
    @pattern.reduce(text) { |newText, op| @opFunctionMap.key?(op) ? opFunctionMap[op].call(newText, false) : newText }
end