《The Ruby Programming Language》5.3.4 Enumerators 这一节中有段代码
def twice
if block_given?
yield
yield
else
self.to_enum(:twice)
end
end
没看懂,原文中说
You can duplicate this behavior in your own iterator methods by returning self.to_enum
when no block is supplied. Here, for example, is a version of the the twice
iterator shown earlier that can return an enumerator if no block is provided:
但是这样写有什么实际意义吗?