Hubro
Hubro

Reputation: 59343

Is there a built in datatype or a gem for table data-structures in Ruby?

I need to represent and work with tables in my Ruby application. By "tables", I mean data structures with columns and rows. I need to be able to remove/append/insert columns and rows, as well as reference cells by row/column. Options for column headers, column types etc. is a plus. I once implemented such a data structure in Python and it ended up just above 1000 lines, so I'd rather use a pre-existing solution.

Are there any built-in data structures or gems that provide this functionality?

Upvotes: 1

Views: 942

Answers (2)

ori danus
ori danus

Reputation: 51

coming from Java I also searched for somthing like Guava's Table , but what about a simple hash in hash like this:

table = {
        'row 1' => { 'column A' => '1A' , 'column B' => '1B' },
        'row 2' => { 'column A' => '2A' , 'column B' => '2B' },
        }

you can access it like:

table['row 1']['column A']

Upvotes: 1

Neil Slater
Neil Slater

Reputation: 27207

If you are happy using SQL DDL to manipulate the structure, and SQL queries to manipulate and extract the data, then you can use a database. That doesn't have to mean client/server, or other large-scale architectures; perhaps a good fit to your requirements is SQLite.

If you make use of SQLite (http://www.sqlite.org/about.html) and the sqlite3 gem, you should also be able to run the database using in-memory mode, if all you want is the data structures it allows during run-time:

require 'sqlite3'
db = SQLite3::Database.new ":memory:"

Upvotes: 2

Related Questions