From: Daniel Berger Date: 2005-05-23T01:10:17+09:00 Subject: Re: join not in Enumerable Logan Capaldo wrote: > Just a few minutes ago I was playing with irb as I am wont to do, and > typed this: > > ('a'..'z').join(' ') > > Lo and behold it protested at me with a NoMethodError. I said to my > self, self there is no reason that has to be Array only functionality. > Why isn't it in Enumerable? So I said: > > module Enumerable > def join(sep = '') > inject do |a, b| > "#{a}#{sep}#{b}" > end > end > end > > And then I said ('a'..'z').join(' ') and got: > => "a b c d e f g h i j k l m n o p q r s t u v w x y z" > > #inject has to be the most dangerously effective method ever. But I digress: > > Why is join, and perhaps even pack in Array and not in Enumerable? Because there is nothing explicitly iterative about join. Also, every class except Array would have to have a custom definition of join, since there's no reasonable default behavior for any class outside of Array. And if every class would have to implement its own version of a method, that method doesn't belong in a module. Modules are not interfaces. I can see that Ara has already found an excuse to give join a block - lovely. The slide continues.... Regards, Dan