[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

problem with gsub! in file

JenC

3/24/2009 1:34:00 PM

I am new to Ruby and trying to do a find and replace on a text file
based on a regular expression. When I step through the code line by
line, it appears that the appropriate line in the file is changed, but
when I exit the function, the file hasn't been modified. Are in-place
edits of files allowed? can you see what it wrong here?

Here is the body of the function:

def ReplaceGuid(guid)
r = nil
sconfProj = File.open("myfile.vcproj", "r+").each do |line|
m = @guidExp.match(line)
if !m.nil?
str = m[1]
r = %r{#{str}}
line.gsub!(r, guid)
break
end
end
end


Many thanks,
Jen
8 Answers

JenC

3/24/2009 2:56:00 PM

0

OK, I see now it's not trivial to read and write from a file at the
same time, and instead I should probably just drop to the command line
and call
`ruby -pe 'gsub(/foo/, "bar")' < myfile.vcproj`

When i do this, the text of the file spits out to stdio, and it
appears the substitution has been made, but when I open the file in a
text editor, it hasn't been changed.
Am I missing something?
I already checked file permissions and everything looks fine

Thanks,
Jen

`

On Mar 24, 2:33 pm, JenC <jcarl...@gmail.com> wrote:
> I am new to Ruby and trying to do a find and replace on a text file
> based on a regular expression.  When I step through the code line by
> line, it appears that the appropriate line in the file is changed, but
> when I exit the function, the file hasn't been modified.  Are in-place
> edits of files allowed? can you see what it wrong here?
>
> Here is the body of the function:
>
> def ReplaceGuid(guid)
>         r = nil
>         sconfProj = File.open("myfile.vcproj", "r+").each do |line|
>                 m = @guidExp.match(line)
>                 if !m.nil?
>                         str = m[1]
>                         r = %r{#{str}}
>                         line.gsub!(r, guid)
>                         break
>                 end
>         end
> end
>
> Many thanks,
> Jen

Adam Gardner

3/24/2009 3:14:00 PM

0

JenC wrote:
> OK, I see now it's not trivial to read and write from a file at the
> same time, and instead I should probably just drop to the command line
> and call
> `ruby -pe 'gsub(/foo/, "bar")' < myfile.vcproj`
>
> When i do this, the text of the file spits out to stdio, and it
> appears the substitution has been made, but when I open the file in a
> text editor, it hasn't been changed.
> Am I missing something?
> I already checked file permissions and everything looks fine
>
> Thanks,
> Jen
>
> `

You'll want ruby -pie 'gsub(/foo/,"bar")' myfile.vcproj

Or, if you want to keep a backup of the original (and you probably do,
until you know this works the way you expect), you can add an extension
of your choice to the -i flag:

ruby -p -i.bak e 'gsub(/foo/,"bar")' myfile.vcproj

I've actually never used this on Windows, you'll have to try it and see.
--
Posted via http://www.ruby-....

Adam Gardner

3/24/2009 3:17:00 PM

0

Adam Gardner wrote:

> ruby -p -i.bak e 'gsub(/foo/,"bar")' myfile.vcproj

Er, sorry, make that ruby -p -i.bak -e 'gsub(/foo/,"bar")'
myfile.vcproj
--
Posted via http://www.ruby-....

JenC

3/24/2009 4:30:00 PM

0

Thanks Adam. I tried this:
ruby -p -i.bak -e 'gsub(/foo/,"bar")' myfile.vcproj

but them I get a whole load of warnings:
-e:1: warning: Can't do inplace edit for stdio

and the file still doesn't change... any ideas?

best,
Jen




On Mar 24, 4:16 pm, Adam Gardner <adam.oddfel...@gmail.com> wrote:
> Adam Gardner wrote:
> > ruby -p -i.bak e 'gsub(/foo/,"bar")'  myfile.vcproj
>
> Er, sorry, make that ruby -p -i.bak -e 'gsub(/foo/,"bar")'
> myfile.vcproj
> --
> Posted viahttp://www.ruby-....

Adam Gardner

3/24/2009 4:46:00 PM

0

JenC wrote:
> Thanks Adam. I tried this:
> ruby -p -i.bak -e 'gsub(/foo/,"bar")' myfile.vcproj
>
> but them I get a whole load of warnings:
> -e:1: warning: Can't do inplace edit for stdio
>
> and the file still doesn't change... any ideas?
>
> best,
> Jen

Are you sure you did just 'myfile.vcproj' and not '< myfile.vcproj'?
--
Posted via http://www.ruby-....

JenC

3/25/2009 8:53:00 AM

0

> Are you sure you did just 'myfile.vcproj' and not '< myfile.vcproj'?
aha! that was it!

Thanks so much!

On Mar 24, 5:46 pm, Adam Gardner <adam.oddfel...@gmail.com> wrote:
> JenC wrote:
> > Thanks Adam.  I tried this:
> > ruby -p -i.bak -e 'gsub(/foo/,"bar")'  myfile.vcproj
>
> > but them I get a whole load of warnings:
> > -e:1: warning: Can't do inplace edit for stdio
>
> > and the file still doesn't change... any ideas?
>
> > best,
> > Jen
>

> --
> Posted viahttp://www.ruby-....

The Bloomfield Bloviator

1/10/2014 5:07:00 AM

0

On Friday, January 10, 2014 12:01:51 AM UTC-5, poisoned rose wrote:

> Well, I'm not a big Turtles fan

Then why are they on your list of favorite acts of the 1960s?

The Bloomfield Bloviator

1/10/2014 6:11:00 AM

0

On Friday, January 10, 2014 12:23:00 AM UTC-5, poisoned rose wrote:

> And in any case, maybe I'm just not a "big fan" of 100 '60s acts. It was a thinner era than the current day due to fewer indie labels, less DIY-level music, etc.

So current music is better than the 60s?

The critics would laugh in your face.

First off, there were LOADS more indie labels in the 60s than there are now.. The reason you don't realize this is your obsession with albums and your disinterest in singles and non white album acts.

Secondly, on AM 10 of the 13 most acclaimed songs of all time are from the 1960s. There are only 4 songs from this century in the 100 most acclaimed songs of all time. Also 9 of the 15 most acclaimed albums of all time are from the 1960s. There are only 4 albums from this century among the 100 most acclaimed albums of all time.

You are delusional if you believe that today's music is better or that there are more good acts now than there wwere then.