# Cleaner way to do this?

**URL:** <https://rubytalk.org/t/cleaner-way-to-do-this/14614>\
**Category:** ruby-talk\
**Created:** [5 November 2004 00:18 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14614 "2004-11-05T00:18:55Z")\
**Posts on this page:** 3\
**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 00:18 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14614/1 "2004-11-05T00:18:55Z")

</div>

Hi,

One of my colleagues asked me last night how he could do some processing  
on every object of an array, with different processing for the first  
item ... but without generating an intermediate variable.

An example would be doing the equivalent of

&nbsp;&nbsp;&nbsp;puts a.join(", ")

without having a temporary string created. The temporary string isn't  
an issue normally, but imagine if the array is huge.

Trying to make it reasonably flexible, I came up with the following, but  
I figure there's probably some cleaner way to do it.

Maybe there's a method I don't know about that allows for this kind of  
thing. Any suggestions?

Pardon the method name ... it's only a model 🙂

class Array  
&nbsp;&nbsp;&nbsp;def first\_and\_rest(first\_time)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first\_time.call self[0]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1 .. self.length - 1).each do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield self[i]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

a = [1, 2, 3, 4, 5]

File.open("test.txt", "w") do |out|  
&nbsp;&nbsp;&nbsp;a.first\_and\_rest(proc {|x| out.print x}) do |x|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.print ", #{x}"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;out.puts  
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:** ![Wayne\_Vucenic1](https://avatars.discourse-cdn.com/v4/letter/w/f17d59/32.png) [@Wayne\_Vucenic1](https://rubytalk.org/u/Wayne_Vucenic1)\
**Post date:** [5 November 2004 01:12 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14614/2 "2004-11-05T01:12:03Z")

</div>

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

Wayne

> **···**
>
> -----  
> No Bugs Software  
> "Ruby and C++ Contract Programming in Silicon Valley"

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [5 November 2004 02:23 UTC](https://rubytalk.org/t/cleaner-way-to-do-this/14614/3 "2004-11-05T02:23:44Z")

</div>

hard to profile with IO involved but:

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;1048576

> **···**
>
> On Fri, 5 Nov 2004, Harry Ohlsen wrote:
> 
> > Hi,
> > 
> > One of my colleagues asked me last night how he could do some processing  
> > on every object of an array, with different processing for the first  
> > item ... but without generating an intermediate variable.
> > 
> > An example would be doing the equivalent of
> > 
> > &nbsp;&nbsp;puts a.join(", ")
> > 
> > without having a temporary string created. The temporary string isn't  
> > an issue normally, but imagine if the array is huge.
> > 
> > Trying to make it reasonably flexible, I came up with the following, but  
> > I figure there's probably some cleaner way to do it.
> > 
> > Maybe there's a method I don't know about that allows for this kind of  
> > thing. Any suggestions?
> > 
> > Pardon the method name ... it's only a model 🙂
> > 
> > class Array  
> > &nbsp;&nbsp;def first\_and\_rest(first\_time)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first\_time.call self[0]
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1 .. self.length - 1).each do |i|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield self[i]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > a = [1, 2, 3, 4, 5]
> > 
> > File.open("test.txt", "w") do |out|  
> > &nbsp;&nbsp;a.first\_and\_rest(proc {|x| out.print x}) do |x|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.print ", #{x}"  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;out.puts  
> > end
> 
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;first\_and\_rest  
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;7.89007091522217  
> &nbsp;&nbsp;&nbsp;549755289600  
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;ruby objects are copy on write references - no copy is made here!  
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;6.67814898490906  
> &nbsp;&nbsp;&nbsp;549755289600  
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;is testing \_\_really\_\_ that slow?  
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;13.0074229240417  
> &nbsp;&nbsp;&nbsp;549755289600
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
> &nbsp;&nbsp;&nbsp;def time label  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts('-' \* 79)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts label  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts('-' \* 79)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = Time::now  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = Time::now  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(b.to\_f - a.to\_f)  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;class Array  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def first\_and\_rest(first\_time)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first\_time.call self[0]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1 .. self.length - 1).each do |i|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield self[i]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;huge = (0...(2 \*\* 20)).to\_a  
> &nbsp;&nbsp;&nbsp;p huge.size
> 
> &nbsp;&nbsp;&nbsp;sum = 0  
> &nbsp;&nbsp;&nbsp;time('first\_and\_rest') do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;huge.first\_and\_rest(proc {|x| sum += x}) do |x|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum += x  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;p sum
> 
> &nbsp;&nbsp;&nbsp;sum = 0  
> &nbsp;&nbsp;&nbsp;time('ruby objects are copy on write references - no copy is made here!') do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;first = huge.first  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum += first  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;huge.last(huge.size - 1).each{|x| sum += x}  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;p sum
> 
> &nbsp;&nbsp;&nbsp;sum = 0  
> &nbsp;&nbsp;&nbsp;time('is testing \_\_really\_\_ that slow?') do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;huge.each\_with\_index do |x, i|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum += (i == 0 ? (x \* 2) : x)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;p sum
> 
> so testing is slow, however i couldn't really tell the difference until a  
> million entries or so. in any case i think your friend isn't clear on the  
> meaning of 'created' - unless you modify the first element no copy is made -  
> you just get a reference which costs basically nothing. to prove it:
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> ruby b.rb  
> &nbsp;&nbsp;&nbsp;1048576  
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;it's plenty fast to take references  
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;3.60012054443359e-05  
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;but slow(er) if you actually copy on write  
> &nbsp;&nbsp;&nbsp;-------------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;36.049026966095
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> cat b.rb  
> &nbsp;&nbsp;&nbsp;def time label  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts('-' \* 79)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts label  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts('-' \* 79)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = Time::now  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b = Time::now  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(b.to\_f - a.to\_f)  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;huge = '\_' \* (2 \*\* 20)  
> &nbsp;&nbsp;&nbsp;p huge.size
> 
> &nbsp;&nbsp;&nbsp;time("it's plenty fast to take references"){ 42.times{ s = huge } }  
> &nbsp;&nbsp;&nbsp;time("but slow(er) if you actually copy on write"){ 42.times{ (s = huge).gsub(%r/./,'!') } }
> 
> IMHO the copy on write semantics of ruby assignment is one of it's nicest  
> features - it's a lot easier to shoot your self in the foot with memory  
> allocation in perl.
> 
> kind regards.
> 
> # -a --
> 
> > EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov  
> > PHONE :: 303.497.6469  
> > When you do something, you should burn yourself completely, like a good  
> > bonfire, leaving no trace of yourself. --Shunryu Suzuki
> 
> ===============================================================================
