現在我發現在文本串的匹配:合併兩個非常相似的紅寶石if語句
if w.include? 'syn'
w.sub!(/.*?syn\|/, '')
return w unless similar?(word, w)
elsif w.include? 'sim'
w.sub!(/.*?sim\|/, '')
return w unless similar?(word, w)
end
但我希望能夠把它們結合在一起,就像我在另一個小程序做了什麼(這只是它的片段):
def plus
self.meta_reduce(:+)
end
def minus
self.meta_reduce(:-)
end
def times
self.make_float
self.meta_reduce(:*)
end
def divide
self.make_float
self.meta_reduce(:/)
end
def make_float args = nil
return args.to_f if args
@value = @value.map {|n| n.to_f }
end
def meta_reduce operation = :sym
if @value.length < 2
raise 'calculator is empty'
end
@value = self.push(@value.pop(2).reduce(operation))
end
我知道,在我的數學例子,在方法的不同之處在於操作適用,而在其他的區別是什麼被匹配。是否有一種優雅的方式通過提供子字符串來即時創建新方法?
你究竟把什麼意思結合起來呢?我無法遵循你想要做的事情。 – squiguy 2013-05-11 22:54:18
我認爲當他說*聯合*他真的意味着*重構* – 2013-05-12 01:24:02
是的,我的意思是重構。 – Emil 2013-05-13 17:03:35