# Newb: Search & Replace

**URL:** <https://www.ruby-forum.com/t/newb-search-replace/51437>\
**Category:** Ruby\
**Created:** [December 31, 2005, 9:32am UTC](https://www.ruby-forum.com/t/newb-search-replace/51437 "2005-12-31T09:32:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![art](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8797f3/32.png) [@art](https://www.ruby-forum.com/u/art)\
**Post date:** [December 31, 2005, 9:32am UTC](https://www.ruby-forum.com/t/newb-search-replace/51437/1 "2005-12-31T09:32:25Z")

</div>

Hi all,

I’m implementing build automation in Ruby as a ‘learn Ruby’ exercise.  
I wanted to post the following code and solicit feedback on any idioms  
I might have missed with regards to text processing with Ruby. The  
code works–I’m just wondering if it should be shorter, or smarter,  
or…

Thanks,

Art

* * *

#!/usr/bin/env ruby

VERSION\_HEX=ARGV[0]

File.open(“iDrum\_prefix.h.tmp”, “w”) do |ofile|  
File.open(“iDrum\_prefix.h”) do |file|  
file.each do |line|  
if line =~ /^#define kIDComponentVersion\t\*(.\*)/  
ofile.puts line.sub($1, “#{VERSION\_HEX}”)  
else  
ofile.puts line  
end  
end  
end  
end

File.rename(“iDrum\_prefix.h.tmp”, “iDrum\_prefix.h”)

---

<div class="post-metadata">

**Author:** ![art](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8797f3/32.png) [@art](https://www.ruby-forum.com/u/art)\
**Post date:** [December 31, 2005, 12:57pm UTC](https://www.ruby-forum.com/t/newb-search-replace/51437/2 "2005-12-31T12:57:38Z")

</div>

On Sat, 31 Dec 2005, Art G. wrote:

> ```
> ofile.puts line
> end
> end
> 
> ```
> 
> end  
> end
> 
> File.rename(“iDrum\_prefix.h.tmp”, “iDrum\_prefix.h”)

with rio, you can edit a file inplace;  
this changes all x into y in file z:

#!/usr/bin/ruby  
require ‘rubygems’  
require ‘rio’  
rio(‘z’) \< rio(‘z’).read.gsub(/x/,‘y’)

---

<div class="post-metadata">

**Author:** ![art](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8797f3/32.png) [@art](https://www.ruby-forum.com/u/art)\
**Post date:** [December 31, 2005, 2:34pm UTC](https://www.ruby-forum.com/t/newb-search-replace/51437/3 "2005-12-31T14:34:00Z")

</div>

Wybo D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > ofile.puts line.sub($1, "#{VERSION_HEX}")
> > 
> > ```
> 
> this changes all x into y in file z:
> 
> #!/usr/bin/ruby  
> require ‘rubygems’  
> require ‘rio’  
> rio(‘z’) \< rio(‘z’).read.gsub(/x/,‘y’)

Um, rio is not needed:

Robert@Babelfish2 /c/TEMP  
$ echo ‘x’ \> iDrum\_prefix.h

Robert@Babelfish2 /c/TEMP  
$ cat iDrum\_prefix.h  
x

Robert@Babelfish2 /c/TEMP  
$ ruby -i.tmp -pe “gsub /x/, ‘y’” iDrum\_prefix.h

Robert@Babelfish2 /c/TEMP  
$ cat iDrum\_prefix.h  
y

:-))

```
robert

```

---

<div class="post-metadata">

**Author:** ![art](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8797f3/32.png) [@art](https://www.ruby-forum.com/u/art)\
**Post date:** [December 31, 2005, 2:58pm UTC](https://www.ruby-forum.com/t/newb-search-replace/51437/4 "2005-12-31T14:58:19Z")

</div>

On Sat, 31 Dec 2005, Robert K. wrote:

> > > if line =~ /^#define kIDComponentVersion\t\*(.\*)/  
> > > with rio, you can edit a file inplace;  
> > > $ echo ‘x’ \> iDrum\_prefix.h  
> > > y
> 
> :-))
> 
> robert

Uhm… ruby is not needed:

sed -ie ‘s/x/y/g’ iDrum\_prefix.h

But I suppose that Art G. wanted to arrange things from a Ruby  
script, where he does other things as well… Happy New Year!

---

<div class="post-metadata">

**Author:** ![art](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8797f3/32.png) [@art](https://www.ruby-forum.com/u/art)\
**Post date:** [December 31, 2005, 5:28pm UTC](https://www.ruby-forum.com/t/newb-search-replace/51437/5 "2005-12-31T17:28:46Z")

</div>

On 12/31/05, Art G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi all,
> 
> I’m implementing build automation in Ruby as a ‘learn Ruby’ exercise.  
> I wanted to post the following code and solicit feedback on any idioms  
> I might have missed with regards to text processing with Ruby. The  
> code works–I’m just wondering if it should be shorter, or smarter,  
> or…

That is nice and idiomatic from my perspective. In fact I’ve written  
code much like that to do what you are doing. While there might be  
various tricks and hacks to make it shorter, they probably aren’t  
needed.

Ryan

---

<div class="post-metadata">

**Author:** ![art](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8797f3/32.png) [@art](https://www.ruby-forum.com/u/art)\
**Post date:** [December 31, 2005, 8:17pm UTC](https://www.ruby-forum.com/t/newb-search-replace/51437/6 "2005-12-31T20:17:51Z")

</div>

Thanks for the replies, everyone. I wound up with

def do\_substitution( filename )  
File.open("#{filename}.tmp", “w”) do |ofile|  
File.open("#{filename}") do |file|  
file.each do |line|  
yield ofile, line  
end  
end  
end  
File.rename("#{filename}.tmp", “#{filename}”)  
end

Which is probably overkill for my needs, but got me learning about  
blocks and is flexible. And fun!

Thanks again.

Art
