Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #7398
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: editing file in ruby |
| Date | 2018-07-21 18:25 +0200 |
| Message-ID | <frh533Fra2tU1@mid.individual.net> (permalink) |
| References | <7df54206-cde1-4dc7-a66c-190f0dc892a8@googlegroups.com> <04a7aba2-d2ee-4e59-95d4-462a2994863d@googlegroups.com> |
On 21.07.2018 17:33, Mohan Mohta wrote:
> Hello
> I wrote a program but what beats me is the print works fine and gives me the expected output fir it does not write the same to the file
>
> File.open("/tmp/grub.conf","r").each_line do |line|
> file_boot=File.open("/tmp/bootfile","w")
You are not closing this file properly.
Here's how I'd do it
File.open '/tmp/bootfile', 'w' do |out|
File.foreach '/tmp/grub.conf' do |line|
if /^\s+kernel\b/ =~ line
line.gsub! /transparent_hugepage=\w+/, ''
line.sub! /\s+$/, ''
line << ' transparent_hugepage=never'
end
out.puts line
end
end
Cheers
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar
editing file in ruby mohan.mohta@gmail.com - 2018-07-19 14:49 -0700
Re: editing file in ruby Mohan Mohta <mohan.mohta@gmail.com> - 2018-07-21 08:33 -0700
Re: editing file in ruby Robert Klemme <shortcutter@googlemail.com> - 2018-07-21 18:25 +0200
Re: editing file in ruby Mohan Mohta <mohan.mohta@gmail.com> - 2018-07-22 12:51 -0700
Re: editing file in ruby Robert Klemme <shortcutter@googlemail.com> - 2018-07-23 22:41 +0200
csiph-web