Question

How to compare the hash output of DB.fetch with an input array?

e.g. if on dataset from hash output of DB.fetch,
dataset.each{|r| p r.values} outputs arrays for each row but what if I want to store these arrays and compare these arrays and and also run computations outside of the {} scope with an existing array?

Was it helpful?

Solution

Your dataset.each{|r| p r.values} returns not array, it prints them.

To get an array with array you could use:

DB[:yourtab].all.map{|v|v.values}

A more complete example:

require 'sequel'
DB = Sequel.sqlite
DB.create_table(:tab1){
  primary_key :id
  field :a, :type => :nvarchar, :size => 10
  field :b, :type => :nvarchar, :size => 10
}

DB[:tab1].insert(:a => 'a1', :b => 'b1')
DB[:tab1].insert(:a => 'a2', :b => 'b2')
DB[:tab1].insert(:a => 'a3', :b => 'b3')

result = DB[:tab1].all.map{|v|v.values}
p result

Result is:

[[1, "a1", "b1"], [2, "a2", "b2"], [3, "a3", "b3"]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top