# Memo to self

**URL:** https://rubytalk.org/t/memo-to-self/25064
**Category:** ruby-talk
**Created:** [20 February 2006 02:18 UTC](https://rubytalk.org/t/memo-to-self/25064 "2006-02-20T02:18:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)
#### Post date: [20 February 2006 02:18 UTC](https://rubytalk.org/t/memo-to-self/25064/1 "2006-02-20T02:18:14Z")

</div>

Never under estimate what you can achieve with  
&nbsp;&nbsp;IO.read( "filename").scan(%r{}mx) do |match|  
end

Especial as you can control how greedy the regex is with .\*?

I just deleted a lot of code and replaced it with one line.

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : [john.carter@tait.co.nz](mailto:john.carter@tait.co.nz)  
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

---

<div class="post-metadata">

### Author: ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)
#### Post date: [20 February 2006 02:20 UTC](https://rubytalk.org/t/memo-to-self/25064/2 "2006-02-20T02:20:55Z")

</div>

John Carter wrote:

> Never under estimate what you can achieve with  
> IO.read( "filename").scan(%r{}mx) do |match|  
> end
> 
> Especial as you can control how greedy the regex is with .\*?
> 
> I just deleted a lot of code and replaced it with one line.

Well, as your self.memo included the rest of us, how about  
sharing the example?

Hal

---

<div class="post-metadata">

### Author: ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)
#### Post date: [20 February 2006 11:38 UTC](https://rubytalk.org/t/memo-to-self/25064/3 "2006-02-20T11:38:32Z")

</div>

John Carter wrote:

> Never under estimate what you can achieve with  
> &nbsp;&nbsp;IO.read( "filename").scan(%r{}mx) do |match|  
> end
> 
> Especial as you can control how greedy the regex is with .\*?
> 
> I just deleted a lot of code and replaced it with one line.
> 
> John Carter Phone : (64)(3) 358 6639  
> Tait Electronics Fax : (64)(3) 359 4632  
> PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
> New Zealand
> 
> Carter's Clarification of Murphy's Law.
> 
> "Things only ever go right so that they may go more spectacularly wrong later."
> 
> From this principle, all of life and physics may be deduced.

# Read and parse the first file on the command line.  
gets(nil).scan( //mx ) { |s|  
}  
# Read and parse the rest of the files.  
ARGF.read.scan( //mx ) { |s|  
}

---

<div class="post-metadata">

### Author: ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)
#### Post date: [23 February 2006 01:19 UTC](https://rubytalk.org/t/memo-to-self/25064/4 "2006-02-23T01:19:35Z")

</div>

Ok. Just quickly. I had a file of the format...

blah  
Command :  
some stuff  
some more stuff I \  
want with line continuation \  
characters

blah  
Command :  
more some stuff  
more some more stuff I \  
want with line continuation \  
characters

blah  
...

that I wanted transformed into  
some stuff  
some more stuff I want with line continuation characters  
more some stuff  
more some more stuff I want with line continuation characters

So I started write a routine to fix up the \ characters yield fixed lines and then a state machine to search for 'Command:' and gather everything until the blank line and then skip until...

I start playing with Generator since I realised I could then keep everything in a  
&nbsp;&nbsp;&nbsp;each\_something do |thing|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield other\_thing( thing)  
&nbsp;&nbsp;&nbsp;end  
pattern instead of doing state machines and then I threw everything away and replaced it with..

&nbsp;&nbsp;&nbsp;&nbsp;File.read( "big\_file").scan(%r{\nCommand\ :\n(.\*?)\n\n}mx) do |command|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts $1.gsub( %r{\\\n}mx, '')  
&nbsp;&nbsp;&nbsp;&nbsp;end

and then  
&nbsp;&nbsp;&nbsp;everyone.send( :memo, "to self")

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

> **···**
>
> On Mon, 20 Feb 2006, Hal Fulton wrote:
> 
> > John Carter wrote:
> > 
> > > Never under estimate what you can achieve with  
> > > IO.read( "filename").scan(%r{}mx) do |match|  
> > > end
> > > 
> > > Especial as you can control how greedy the regex is with .\*?
> > > 
> > > I just deleted a lot of code and replaced it with one line.
> > 
> > Well, as your self.memo included the rest of us, how about  
> > sharing the example?
