def check if a == b || c == b # execute some code # b = the same variable end end
这可以写得像
def check if a || c == b # this doesn't do the trick end if (a || c) == b # this also doesn't do the magic as I thought it would end end
或者以某种方式我不需要输入两次b.这是懒惰,我想知道.
if [a,c].include? b # code end
但是,这比你想要避免的代码要慢得多 – 至少只要a,b和c是基本数据.我的测量结果显示因子为3.这可能是由于创建了额外的Array对象.所以你可能不得不在这里权衡DRY与性能.通常情况下,这并不重要,因为两种变体都不会花费很长时间.