Commit 934b2082 authored by Robert Chatley's avatar Robert Chatley
Browse files

remove duplication using Template Method Pattern

parent 8f39871a
class ReverseEncoder
class LineEncoder
def encode(str)
words = str.split(" ")
......@@ -10,23 +10,23 @@ class ReverseEncoder
end
def encode_word(word)
word.reverse
raise "Subclasses should define encode_word(word)"
end
end
class DoublingEncoder
class ReverseEncoder < LineEncoder
def encode(str)
words = str.split(" ")
result = []
for word in words
result << encode_word(word)
end
result.join(" ")
def encode_word(word)
word.reverse
end
end
class DoublingEncoder < LineEncoder
def encode_word(word)
word * 2
end
end
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment