Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 789357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:30:43+00:00 2026-05-14T21:30:43+00:00

I need to save a few informations about some files. Nothing too fancy so

  • 0

I need to save a few informations about some files. Nothing too fancy so I thought I would go with a simple one line per item text file. Something like this :

# write
io.print "%i %s %s\n" % [File.mtime(fname), fname, Digest::SHA1.file(fname).hexdigest]
# read
io.each do |line|
  mtime, name, hash = line.scanf "%i %s %s"
end

Of course this doesn’t work because a file name can contain spaces (breaking scanf) and line breaks (breaking IO#each).

The line break problem can be avoided by dropping the use of each and going with a bunch of gets(‘ ‘)

while not io.eof?
  mtime = Time.at(io.gets(" ").to_i)
  name = io.gets " "
  hash = io.gets "\n"
end

Dealing with spaces in the names is another matter. Now we need to do some escaping.
note : I like space as a record delimiter but I’d have no issue changing it for one easier to use. In the case of filenames though, the only one that could help is ascii nul “\0” but a nul delimited file isn’t really a text file anymore…

I initially had a wall of text detailing the iterations of my struggle to make a correct escaping function and its reciprocal but it was just boring and not really useful. I’ll just give you the final result:

def write_name(io, val)
  io << val.gsub(/([\\ ])/, "\\\\\\1") # yes that' 6 backslashes !
end

def read_name(io)
  name, continued = "", true
  while continued
    continued = false
    name += io.gets(' ').gsub(/\\(.)/) do |c|
      if c=="\\\\"
        "\\"
      elsif c=="\\ "
        continued=true
        " "
      else
        raise "unexpected backslash escape  : %p (%s %i)" % [c, io.path, io.pos]
      end
    end
  end
  return name.chomp(' ')
end

I’m not happy at all with read_name. Way too long and akward, I feel it shouldn’t be that hard.

While trying to make this work I tried to come up with other ways :

  • the bittorrent encoded / php serialize way : prefix the file name with the length of the name then just io.read(name_len.to_i). It works but it’s a real pita to edit the file by hand. At this point we’re halfway to a binary format.

  • String#inspect : This one looks expressly made for that purpose ! Except it seems like the only way to get the value back is through eval. I hate the idea of eval-ing a string I didn’t generate from trusted data.

So. Opinions ? Isn’t there some lib which can do all this ? Am I missing something obvious ? How would you do that ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:30:44+00:00Added an answer on May 14, 2026 at 9:30 pm

    When you say “save” do you mean store the information in a file?

    You could use the CSV module from the Ruby Standard Library. This would mean that your delimiter is comma rather than space but it would handle all the escaping and unescaping for you.

    • If a value contains spaces that value is enclosed in "quotes"

    • If a value contains quotes then a quote character is escaped as 2 quote characters e.g. "hello" would become """hello"""

    To write the details to a file:

    require 'csv'
    
    outfile = File.open('csvout', 'wb')
    CSV::Writer.generate(outfile) do |csv|
      csv << [File.mtime(fname), fname, Digest::SHA1.file(fname).hexdigest]
    end
    outfile.close
    

    To read them back:

    CSV::Reader.parse(File.open('csvout', 'rb')) do |row|
      p row
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to save some classes and data structures to a file. My first
I need to save files with non-latin filenames on a filesytem, using PHP. I
I have one dictionary I need to save into a plist. The paletteDictionary always
I have a very simple web project, which need to just persistence some users,
I need to store few objects (Not simple Type) to sharepoint and access them
I need to save a list of records with a few columns. I got
I need to save a few things in the session. At the moment, I'm
(sorry for my english) I need save an image in the application path and
I need to save the context of the program before exiting ... I've put
I need to save images in certain folder of my application: string path=E:\AJAXEnabledWebSite1\images\gallery I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.