Hi -- On Sun, 22 May 2005, 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: You can speed it up a lot if you do this: module Enumerable def join(sep = '') to_a.join(sep) end end Benchmarking 10 calls to each version, for a dummy class where each just iterates from 1 to 1000: user system total real inject 2.720000 0.030000 2.750000 ( 2.759071) to_a 0.300000 0.000000 0.300000 ( 0.298650) > Why is join, and perhaps even pack in Array and not in Enumerable? I guess to_a makes the conversion pretty easy, and Array tends to serve as the "normalized" version of Enumerable in a lot of contexts. I don't know if there's any other reason. David -- David A. Black dblack / wobblini.net