Newb: Search & Replace

A

Art Gillespie

Hi all,

I'm implementing build automation in Ruby as a 'learn Ruby' exercise.=20
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=3DARGV[0]

File.open("iDrum_prefix.h.tmp", "w") do |ofile|
File.open("iDrum_prefix.h") do |file|
file.each do |line|
if line =3D~ /^#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")
 
W

Wybo Dekker

#!/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")

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')
 
R

Robert Klemme

Wybo Dekker said:
#!/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")

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')

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
 
W

Wybo Dekker

Wybo Dekker said:
#!/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")

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')

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

Uhm... ruby is not needed:

sed -ie 's/x/y/g' iDrum_prefix.h

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

Ryan Leavengood

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
 
A

Art Gillespie

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top