On Mon, 23 May 2005, Daniel Berger wrote:

> 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.

   Enumerable#join(sep): concatinate each (Enumerable#each) thing onto a string
   followed by sep, unless it is the last (implying iteration) thing.

isn't this definition reasonable and iterative?

> 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.

really?

   set = Set::new
   set.join ','

   ll = LinkedList::new
   ll.join '->'

   dll = DoublyLinkedList::new
   dll.join '<->'

   v = BitVector::new
   v.join '|'

   path = graph.shortest_path from, to
   path.join '=>'

   string = String::new
   string.join "<br>"

   stack = Stack::new
   stack.join '-'

   rope = Rope::new
   rope.join '_'

   priority_queue = PriorityQueue::new
   priority_queue.join(','){|priority_and_obj| priority_and_obj.join ':'}

come to mind ;-)


> 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.

why would every class have to implement it's own?  with the defintion we've
been throwing around we already have things like

   harp:~/build/ruby > ./ruby -e'html = "line1\nline2\nline3".join "<br>"; p html'
   "line1\n<br>line2\n<br>line3"

which is kinda handy and makes good sense no?

> I can see that Ara has already found an excuse to give join a block -
> lovely.  The slide continues....

weee.  ;-)

cheers.

-a
-- 
===============================================================================
| 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
===============================================================================