the only reason i can think of is that just because somthing is countable
(Enumerable) doesn't mean each sub-thing is singular. take a hash for
example. this is no stubling block (pun intended) for ruby however:
harp:~ > cat a.rb
module Enumerable
def join(sep = '', &b)
inject(nil){|s,x| "#{ s }#{ s && sep }#{ b ? b[ x ] : x }"}
end
end
class Array; def join(*a, &b); super; end; end
r = 'a' .. 'z'
p(r.join(' '))
h = {:k => :v, :K => :V}
p(h.join(';'){|kv| kv.join '=>'})
a = [ [0, 1], [2, 3] ]
p(a.join(','){|kv| kv.join ':'})
harp:~ > ruby a.rb
"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"
"k=>v;K=>V"
"0:1,2:3"
this allows 'nesting' of join calls for arbitrarily deep enumerable structures.
a3 = [
[ [:a, :b], [:c, :d] ],
[ [:e, :f], [:g, :h] ],
]
p( a3.join('___'){|a2| a2.join('__'){|a1| a1.join '_'}} )
#=> "a_b__c_d___e_f__g_h"
it's a nice idea you have there!
cheers.
-a
···
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:
Why is join, and perhaps even pack in Array and not in Enumerable?
--
email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
My religion is very simple. My religion is kindness.
--Tenzin Gyatso
===============================================================================