dog = 'animal' cat = 'animal' chicken = 'animal' shoe = 'clothing' # Something like this...which doesn't work dog == cat == chicken # true dog == cat == shoe # false
dog == cat && cat == chicken
当然,如果你想……你总能变得聪明……
[dog,cat,chicken] == [dog] * 3 [dog,chicken].uniq.length == 1
…但这并没有真正使代码更简洁(或可读).
如果你想要一个可以比较任意多个元素的可重用函数,我会做这样的事情:
def all_equal?(*elements) elements.all? { |x| x == elements.first } end