On Dec 31, 2006, at 4:16 PM, Ryan Davis wrote:

>
> On Dec 31, 2006, at 12:05 PM, James Edward Gray II wrote:
>
>> I've been looking at the to_sentence() method in Rails.  It's  
>> basically a join(), but you can give a different final separator.   
>> I personally use this kind of functionality often enough to  
>> believe it would make a good addition to the core language.
>>
>> Even better, I think we can make join() smart enough to eliminate  
>> any need for to_sentence() by adding a second argument.  Here's a  
>> sample implementation:
>>
>> #!/usr/bin/env ruby -w
>>
>> require "enumerator"
>>
>> class Array
>>   alias_method :old_join, :join
>>   def join(sep = $,, last_sep = sep)
>>     return "" if empty?
>>     enum_with_index.inject("") do |str, (e, i)|
>>       "#{str}#{i == size - 1 ? last_sep : sep}#{e}"
>>     end[sep.to_s.length..-1]
>>   end
>> end
>
> problems:
>
> 1) calculating a static value inside a loop. (only costs about a  
> hundredth of a second for 10k)
> 2) iterating when 1/2 of your values are completely static.
> 3) being clever by using enumerator.

This was intended as a point of discussion, not my final offer as the  
ideal implementation.  Thank you for cleaning it up though.

James Edward Gray II