Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: editing file in ruby Date: Sat, 21 Jul 2018 18:25:06 +0200 Lines: 30 Message-ID: References: <7df54206-cde1-4dc7-a66c-190f0dc892a8@googlegroups.com> <04a7aba2-d2ee-4e59-95d4-462a2994863d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net iVs3y6+Xfu6i7QDlQEGYwAYC9FONVVztc36n7lLQv0avK1wqc= Cancel-Lock: sha1:plGi62l8VPtHb7gH9/7kvHWcQEA= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 In-Reply-To: <04a7aba2-d2ee-4e59-95d4-462a2994863d@googlegroups.com> Content-Language: en-US Xref: csiph.com comp.lang.ruby:7398 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/