Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete last line of file in Ruby?

Tags:

file

ruby

I am accessing a file, and before I append to it, I want to delete the last line from the file. Is there any efficient way of doing this in Ruby?

This is what I am using to access the file:

file = File.new("imcs2.xml", "a")
like image 770
Javed Ahamed Avatar asked Jul 27 '09 20:07

Javed Ahamed


1 Answers

I'm not gonna try to guess what you're trying to do, but if you're trying to get rid of the closing tag of the root element in an XML file so that you can add more child tags, then re-add the closing root tag, I would imagine that there are ruby modules out there that facilitate this task of writing/editing XML. Just sayin.

Perhaps Builder:

  • Class: Builder::XmlMarkup
  • Project Builder

hpricot also seems to work:

  • Hpricot XML
like image 134
Jorge Israel Peña Avatar answered Sep 30 '22 02:09

Jorge Israel Peña