# Cleaner way to do this?

**URL:** <https://rubytalk.org/t/cleaner-way-to-do-this/14615>\
**Category:** ruby-talk\
**Created:** [5 November 2004 01:24 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14615 "2004-11-05T01:24:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harry\_Ohlsen](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Harry\_Ohlsen](https://rubytalk.org/u/Harry_Ohlsen)\
**Post date:** [5 November 2004 01:24 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14615/1 "2004-11-05T01:24:14Z")

</div>

That's what I initially suggested :-).

However, he felt it was a bit crazy to do the test every time, given the  
large size of the array.

H.

> **···**
>
> > -----Original Message-----  
> > From: Wayne Vucenic [mailto:nightphotos@gmail.com]  
> > Sent: Friday, 5 November 2004 12:12  
> > To: ruby-talk ML  
> > Subject: Re: Cleaner way to do this?
> > 
> > How about
> > 
> > [1, 2, 3, 4, 5].each\_with\_index do |x, i|  
> > &nbsp;&nbsp;&nbsp;if i == 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print x  
> > &nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print ", ",x  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> 
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> 
> If you have received this e-mail in error, please delete it and notify the sender as soon as possible. The contents of this e-mail may be confidential and the unauthorized use, copying, or dissemination of it and any attachments to it, is prohibited.
> 
> Internet communications are not secure and Hyperion does not, therefore, accept legal responsibility for the contents of this message nor for any damage caused by viruses. The views expressed here do not necessarily represent those of Hyperion.
> 
> For more information about Hyperion, please visit our Web site at [www.hyperion.com](http://www.hyperion.com)

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [5 November 2004 03:01 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14615/2 "2004-11-05T03:01:05Z")

</div>

Harry Ohlsen wrote:

> > From: Wayne Vucenic [mailto:nightphotos@gmail.com] Sent: Friday, 5 November 2004 12:12  
> > To: ruby-talk ML  
> > Subject: Re: Cleaner way to do this?
> > 
> > How about
> > 
> > [1, 2, 3, 4, 5].each\_with\_index do |x, i|  
> > &nbsp;&nbsp;if i == 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;print x  
> > &nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;print ", ",x  
> > &nbsp;&nbsp;end  
> > end  
> > &nbsp;&nbsp;&nbsp;  
> > That's what I initially suggested :-).
> 
> However, he felt it was a bit crazy to do the test every time, given the  
> large size of the array.
> 
> H.

If your friend knows the processing he wants to do on the first element, why not just keep it simple...

arr = [1,2,3,4,5]  
process = proc{ |e| print e }.call( arr[0] )  
arr[1...arr.length].each do |e|  
&nbsp;&nbsp;&nbsp;&nbsp;print ", #{e}"  
end

Zach

> **···**
>
> > > -----Original Message-----

---

<div class="post-metadata">

**Author:** ![benny1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@benny1](https://rubytalk.org/u/benny1)\
**Post date:** [5 November 2004 13:48 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14615/3 "2004-11-05T13:48:43Z")

</div>

Harry Ohlsen wrote:

> **···**
>
> > > -----Original Message-----  
> > > From: Wayne Vucenic [mailto:nightphotos@gmail.com]  
> > > Sent: Friday, 5 November 2004 12:12  
> > > To: ruby-talk ML  
> > > Subject: Re: Cleaner way to do this?
> > > 
> > > How about
> > > 
> > > [1, 2, 3, 4, 5].each\_with\_index do |x, i|  
> > > &nbsp;&nbsp;&nbsp;if i == 0  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print x  
> > > &nbsp;&nbsp;&nbsp;else  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print ", ",x  
> > > &nbsp;&nbsp;&nbsp;end  
> > > end
> > 
> > That's what I initially suggested :-).
> > 
> > However, he felt it was a bit crazy to do the test every time, given the  
> > large size of the array.
> 
> how about this?
> 
> arr = [1, 2, 3, 4, 5]  
> first = arr.shift  
> print arr.inject(first) { | c, i | "#{c.to\_s}, #{i.to\_s}" }
> 
> benny

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [5 November 2004 04:21 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14615/4 "2004-11-05T04:21:48Z")

</div>

Zach Dennis wrote:

> If your friend knows the processing he wants to do on the first element, why not just keep it simple...
> 
> arr = [1,2,3,4,5]  
> process = proc{ |e| print e }.call( arr[0] )  
> arr[1...arr.length].each do |e|  
> &nbsp;&nbsp;&nbsp;print ", #{e}"  
> end

This is also faster, then doing a comparison in each iteration :

RESULTS:

---------- Capture Output ----------  
"C:\ruby\bin\ruby.exe" -d harry\_ohlsen.rb  
WRITING 1  
Took:  
19.297000169754  
seconds  
WRITING 2  
Took: 11.7650001049042 seconds  
Terminated with exit code 0

--------CODE-----------  
require 'time'

arr =   
2000000.times do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;arr \<\< "abcdefghijklmnopqrstuvwxyz"  
end

f = File.new( "temp.txt" , "w+" )  
t = Time.new  
arr.each\_with\_index do |x,i|  
&nbsp;&nbsp;&nbsp;&nbsp;if i==0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.print x  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;f.print ', ',x  
&nbsp;&nbsp;&nbsp;&nbsp;end end  
f.close  
puts "WRITING 1"  
puts "Took: ", (Time.new.to\_f - t.to\_f).to\_s, " seconds"

f = File.new( "temp.txt2" , "w+" )  
t = Time.new  
process = proc { |e| f.print e }.call( arr[0] )  
arr[1...arr.length].each do |e|  
&nbsp;&nbsp;&nbsp;&nbsp;f.print ', ',e  
end  
f.close  
puts "WRITING 2"  
print "Took: ", (Time.new.to\_f- t.to\_f).to\_s, " seconds"

---

<div class="post-metadata">

**Author:** ![benny1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@benny1](https://rubytalk.org/u/benny1)\
**Post date:** [5 November 2004 13:58 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14615/5 "2004-11-05T13:58:41Z")

</div>

benny wrote:

> Harry Ohlsen wrote:
> 
> > > From: Wayne Vucenic [mailto:nightphotos@gmail.com]  
> > > Sent: Friday, 5 November 2004 12:12  
> > > To: ruby-talk ML  
> > > Subject: Re: Cleaner way to do this?
> > > 
> > > How about
> > > 
> > > [1, 2, 3, 4, 5].each\_with\_index do |x, i|  
> > > &nbsp;&nbsp;&nbsp;if i == 0  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print x  
> > > &nbsp;&nbsp;&nbsp;else  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print ", ",x  
> > > &nbsp;&nbsp;&nbsp;end  
> > > end
> > 
> > That's what I initially suggested :-).
> > 
> > However, he felt it was a bit crazy to do the test every time, given the  
> > large size of the array.
> 
> how about this?
> 
> arr = [1, 2, 3, 4, 5]  
> first = arr.shift  
> print arr.inject(first) { | c, i | "#{c.to\_s}, #{i.to\_s}" }
> 
> benny

or even

print [1, 2, 3, 4, 5].join(' ,')

benny

> **···**
>
> > > > -----Original Message-----

---

<div class="post-metadata">

**Author:** ![benny1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@benny1](https://rubytalk.org/u/benny1)\
**Post date:** [5 November 2004 14:08 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14615/6 "2004-11-05T14:08:42Z")

</div>

benny wrote:

> arr = [1, 2, 3, 4, 5]  
> first = arr.shift  
> print arr.inject(first) { | c, i | "#{c.to\_s}, #{i.to\_s}" }
> 
> benny

now I saw the initial post 🙂  
sorry for the noise

benny

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [5 November 2004 14:07 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14615/7 "2004-11-05T14:07:00Z")

</div>

Hi --

> **···**
>
> On Fri, 5 Nov 2004, benny wrote:
> 
> > or even
> > 
> > print [1, 2, 3, 4, 5].join(' ,')
> 
> See the very first post in this thread 🙂
> 
> David
> 
> --  
> David A. Black  
> dblack@wobblini.net
