# Idiomatic write

**URL:** <https://rubytalk.org/t/idiomatic-write/1364>\
**Category:** ruby-talk\
**Created:** [23 August 2002 12:32 UTC](https://rubytalk.org/t/idiomatic-write/1364 "2002-08-23T12:32:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Berger\_Daniel2](https://avatars.discourse-cdn.com/v4/letter/b/9fc348/32.png) [@Berger\_Daniel2](https://rubytalk.org/u/Berger_Daniel2)\
**Post date:** [23 August 2002 12:32 UTC](https://rubytalk.org/t/idiomatic-write/1364/1 "2002-08-23T12:32:54Z")

</div>

And doesn’t this secretly generate a tempfile in the process?

Could the OP be looking for something like mmap? (see RAA)

Regards,

Dan

> **···**
>
> > -----Original Message-----  
> > From: Tim Hammerquist [[mailto:tim@vegeta.ath.cx](mailto:tim@vegeta.ath.cx)]
> > 
> > > > In Ruby, I could do the same thing, slurp the file into a  
> > > > string and then  
> > > > write it back again. I was wondering if there’s a more  
> > > > idiomatic way of  
> > > > doing it, in place, i.e., edit the file straight away.  
> > > > This could probably  
> > > > be more practical when the file to be edited is a huge one…
> > > > 
> > > > Any pointers(sic)…
> > > 
> > > % ruby -i~ -pe ‘gsub(/pattern/, “replace”)’ myfile
> > 
> > Does this work if /pattern/ spans lines?
> > 
> > Tim Hammerquist

---

<div class="post-metadata">

**Author:** ![Michael\_Campbell1](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Michael\_Campbell1](https://rubytalk.org/u/Michael_Campbell1)\
**Post date:** [23 August 2002 15:07 UTC](https://rubytalk.org/t/idiomatic-write/1364/2 "2002-08-23T15:07:07Z")

</div>

It does generate a secret tempfile, but the original poster _MIGHT_  
just have wanted to not have to create one himself and be responsible  
for cleaning it up, which this does.

> **···**
>
> — “Berger, Daniel” [djberge@qwest.com](mailto:djberge@qwest.com) wrote:
> 
> > > -----Original Message-----  
> > > From: Tim Hammerquist [[mailto:tim@vegeta.ath.cx](mailto:tim@vegeta.ath.cx)]
> > > 
> > > > > In Ruby, I could do the same thing, slurp the file into a  
> > > > > string and then  
> > > > > write it back again. I was wondering if there’s a more  
> > > > > idiomatic way of  
> > > > > doing it, in place, i.e., edit the file straight away.  
> > > > > This could probably  
> > > > > be more practical when the file to be edited is a huge one…
> > > > > 
> > > > > Any pointers(sic)…
> > > > 
> > > > % ruby -i~ -pe ‘gsub(/pattern/, “replace”)’ myfile
> > > 
> > > Does this work if /pattern/ spans lines?
> > > 
> > > Tim Hammerquist
> > 
> > And doesn’t this secretly generate a tempfile in the process?
> > 
> > Could the OP be looking for something like mmap? (see RAA)
> 
> ## =====
> 
> Use your computer to help find a cure for cancer: [Webspace nicht erreichbar](http://members.ud.com/projects/cancer/)
> 
> Yahoo IM: michael\_s\_campbell
> 
> * * *
> 
> Do You Yahoo!?  
> Yahoo! Finance - Get real-time stock quotes
> 
> > **[Yahoo Finance - Stock Market Live, Quotes, Business & Finance News](https://finance.yahoo.com/)**
> >
> > At Yahoo Finance, you get free stock quotes, up-to-date news, portfolio management resources, international market data, social interaction and mortgage rates that help you manage your financial life.
