Save / edit array inside and outside ruby

I have an array like "author", "post title", "date", "time", "post category" etc. etc.

I clear the details from the forum and I want

  • save data with ruby
  • update data with ruby
  • update data using a text editor or was I thinking of one of the OpenOffice programs? Calc would be the best.

I assume some kind of SQL database will be the solution, but I need a quick solution for this (something that I can do on my own :-)

any suggestions?

thank

+3
source share
4 answers

, csv - . stdlib csv api like:

require 'csv'

my2DArray = [[1,2],["foo","bar"]]

File.open('data.csv', 'w') do |outfile|
  CSV::Writer.generate(outfile) do |csv|
    my2DArray.each do |row|
      csv << row
    end
  end
end

calc .

API ruby, .

+2

YAML - .

require "yaml"
yaml= ["author","post title","date","time","post category"].to_yaml
File.open("filename", "w") do |f|
  f.write(yaml)
end

---
- author
- post title
- date
- time
- post category

,

require "yaml"
YAML.load(File.read("filename")) # => ["author","post title","date","time","post category"]

Yaml , ( ooffice). . Yaml ruby, . itrodution yaml: http://yaml.kwiki.org/?YamlInFiveMinutes.

+7

json . .

if you want to edit it in a way like calc, you might consider creating a CSV file (comma separated values) and import it.

+1
source

If I understand correctly, you have a two-dimensional array. You can output it in csv format as follows:

array.each do |row|
    puts row.join(",")
end

Then you import it using Calc to edit it or just use a text editor.

If your data may contain commas, you should look at the csv module: http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

0
source

Source: https://habr.com/ru/post/1728718/


All Articles