You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
880 B
53 lines
880 B
2 years ago
|
def levenshtein(s1, s2)
|
||
|
a1 = (0..s2.length).to_a
|
||
|
a2 = []
|
||
|
|
||
|
s1.chars.each_with_index do |ch1, i|
|
||
|
a2[0] = i + 1
|
||
|
|
||
|
s2.chars.each_with_index do |ch2, j|
|
||
|
cost = ch1 == ch2 ? 0 : 1
|
||
|
a2[j + 1] = [a2[j] + 1, a1[j + 1] + 1, a1[j] + cost].min
|
||
|
end
|
||
|
a1 = a2.dup
|
||
|
end
|
||
|
a1[s2.length]
|
||
|
end
|
||
|
|
||
|
class TypoBomb
|
||
|
|
||
|
def get_stuff
|
||
|
"My method is working as expected"
|
||
|
end
|
||
|
|
||
|
def method_missing(method_name, *args, &block)
|
||
|
name = methods.select do |name|
|
||
|
levenshtein(name.to_s, method_name.to_s) <= 2
|
||
|
end.first
|
||
|
|
||
|
if !name.nil?
|
||
|
val = send(name, *args, &block)
|
||
|
self.class.undef_method(name)
|
||
|
return val
|
||
|
else
|
||
|
raise NoMethodError
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class InheritedBomb < TypoBomb
|
||
|
def get_favorite_number
|
||
|
5
|
||
|
end
|
||
|
end
|
||
|
|
||
|
class CalculatorBomb < TypoBomb
|
||
|
def add(x, y)
|
||
|
x + y
|
||
|
end
|
||
|
|
||
|
def subtract(x, y)
|
||
|
x - y
|
||
|
end
|
||
|
end
|