In-place editing

I’m trying to figure out why the following code chunk fails to edit my
file in-place:

$-i = ‘.foo’ # set in-place edit mode
mf = File.open(‘Makefile’, ‘r+’)
mf.each_line do |line|
line.sub!(/-l$(RUBY_INSTALL_NAME) /, “”)
end
mf.close

Any ideas?

Ian

···


Ian Macdonald | Life is like an onion: you peel off layer
ian@caliban.org | after layer and then you find there is
> nothing in it. – James Huneker
>
>

Hi,

···

At Wed, 19 Jun 2002 07:31:53 +0900, Ian Macdonald wrote:

I’m trying to figure out why the following code chunk fails to edit my
file in-place:

$-i = ‘.foo’ # set in-place edit mode
mf = File.open(‘Makefile’, ‘r+’)
mf.each_line do |line|
line.sub!(/-l$(RUBY_INSTALL_NAME) /, “”)
end
mf.close

Inplace edit works for ARGF.

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
print line.sub(/-l$(RUBY_INSTALL_NAME) /, “”)
end


Nobu Nakada

Thank you very much.

I didn’t find anything in the documentation to suggest that it only
works on ARGF, so it would have taken me a very long time to figure
that out.

Ian

···

On Wed 19 Jun 2002 at 08:22:09 +0900, nobu.nokada@softhome.net wrote:

Inplace edit works for ARGF.

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
print line.sub(/-l$(RUBY_INSTALL_NAME) /, “”)
end


Ian Macdonald | If we all work together, we can totally
ian@caliban.org | disrupt the system.
>
>
>

what if one needed to make several changes through out the file?

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
print line.sub(/blah1/, “blah2”)
print line.sub(/blah3/, “blah4”)
end

this seems to give me the correct substitutions…but twice as
many lines :slight_smile:

todd

···

On Wed, Jun 19, 2002 at 08:22:09AM +0900, nobu.nokada@softhome.net wrote:

Hi,

At Wed, 19 Jun 2002 07:31:53 +0900, > Ian Macdonald wrote:

I’m trying to figure out why the following code chunk fails to edit my
file in-place:

$-i = ‘.foo’ # set in-place edit mode
mf = File.open(‘Makefile’, ‘r+’)
mf.each_line do |line|
line.sub!(/-l$(RUBY_INSTALL_NAME) /, “”)
end
mf.close

Inplace edit works for ARGF.

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
print line.sub(/-l$(RUBY_INSTALL_NAME) /, “”)
end


Nobu Nakada


“This UI has been brought to you by the letters ‘S’ and ‘K’, and the runlevel 3.”
- Greg Andrews

Ian Macdonald ian@caliban.org writes:

I didn’t find anything in the documentation to suggest that it only
works on ARGF, so it would have taken me a very long time to figure
that out.

Ian:

It’s under ‘Command-Line Options’ on page 138 of the Pickaxe:

Edits ARGV files in place. For each file named in ARGV, anything you
write to standard output will be saved back as the contents of that
file. A backup copy of the file will be made if extension is
supplied.

Cheers

Dave

Just avoid the first print:

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
line.sub!(/blah1/, “blah2”)
print line.sub(/blah3/, “blah4”)
end

Ian

···

On Thu 20 Jun 2002 at 03:54:38 +0900, Todd Holloway wrote:

what if one needed to make several changes through out the file?

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
print line.sub(/blah1/, “blah2”)
print line.sub(/blah3/, “blah4”)
end

this seems to give me the correct substitutions…but twice as
many lines :slight_smile:


Ian Macdonald | Men of quality are not afraid of women for
ian@caliban.org | equality.
>
>
>

Because you have two prints for each line. Why not something like:

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
line.gsub!(/blah1/, “blah2”)
line.gsub!(/blah3/, “blah4”)
print line
end

···

On Wednesday 19 June 2002 11:54 am, Todd Holloway wrote:

what if one needed to make several changes through out the file?

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
print line.sub(/blah1/, “blah2”)
print line.sub(/blah3/, “blah4”)
end

this seems to give me the correct substitutions…but twice as
many lines :slight_smile:


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

Ah, I see. I didn’t realise that the $-i variable worked the same way.

I really should have remembered this from Perl, since that also
requires you to set @ARGV to the list of files to be edited.

Thanks for the pointer.

Ian

···

On Wed 19 Jun 2002 at 09:43:45 +0900, Dave Thomas wrote:

Ian Macdonald ian@caliban.org writes:

I didn’t find anything in the documentation to suggest that it only
works on ARGF, so it would have taken me a very long time to figure
that out.

Ian:

It’s under ‘Command-Line Options’ on page 138 of the Pickaxe:

Edits ARGV files in place. For each file named in ARGV, anything you
write to standard output will be saved back as the contents of that
file. A backup copy of the file will be made if extension is
supplied.


Ian Macdonald | How to Raise Your I.Q. by Eating Gifted
ian@caliban.org | Children – Book title by Lewis B.
> Frumkes
>

thanks…I had tried that, but I forgot the “!” in the previous line.

todd

···

On Thu, Jun 20, 2002 at 04:01:56AM +0900, Ian Macdonald wrote:

On Thu 20 Jun 2002 at 03:54:38 +0900, Todd Holloway wrote:

what if one needed to make several changes through out the file?

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
print line.sub(/blah1/, “blah2”)
print line.sub(/blah3/, “blah4”)
end

this seems to give me the correct substitutions…but twice as
many lines :slight_smile:

Just avoid the first print:

ARGV.replace([‘Makefile’])
ARGF.each_line do |line|
line.sub!(/blah1/, “blah2”)
print line.sub(/blah3/, “blah4”)
end

Ian

Ian Macdonald | Men of quality are not afraid of women for
ian@caliban.org | equality.
>
>
>


“This UI has been brought to you by the letters ‘S’ and ‘K’, and the runlevel 3.”
- Greg Andrews