Привет, ребята, я пытаюсь распечатать элементы ha sh в файл, используя table-print
, но этот tp
будет печатать каждый ключ в столбце, но я хочу, чтобы все keys
были в одном столбце с именем words
и всеми значениями в другом столбце с именем num
.
так вот что я написал до сих пор:
class Xclass
require 'table_print'
attr_accessor :dic
def initialize()
@dic = { apple: 1, water: 2 , orange: 3 }
end
def getkeys
dic.keys.sort.each do |key|
return key
end
end
def getvalue
dic.values.sort.each do |value|
return value
end
end
def nice
f = File.open("users.txt", "w")
tp.set :io, f
#tp [dic], words:getkeys , num:getvalue #just a helpless try generate the same error but `merge' for :apple:Symbol
tp [dic], words:dic.keys , num:dic.values
end
end
Xclass.new().nice()
все же этот код генерирует следующую ошибку:
10: from table.rb:27:in `<main>'
9: from table.rb:23:in `nice'
8: from /var/lib/gems/2.5.0/gems/table_print-1.5.6/lib/table_print.rb:73:in `tp'
7: from /var/lib/gems/2.5.0/gems/table_print-1.5.6/lib/table_print.rb:34:in `table_print'
6: from /var/lib/gems/2.5.0/gems/table_print-1.5.6/lib/table_print.rb:65:in `columns'
5: from /var/lib/gems/2.5.0/gems/table_print-1.5.6/lib/table_print.rb:65:in `new'
4: from /var/lib/gems/2.5.0/gems/table_print-1.5.6/lib/table_print/config_resolver.rb:13:in `initialize'
3: from /var/lib/gems/2.5.0/gems/table_print-1.5.6/lib/table_print/config_resolver.rb:41:in `process_option_set'
2: from /var/lib/gems/2.5.0/gems/table_print-1.5.6/lib/table_print/config_resolver.rb:41:in `collect'
1: from /var/lib/gems/2.5.0/gems/table_print-1.5.6/lib/table_print/config_resolver.rb:41:in `block in process_option_set'
/var/lib/gems/2.5.0/gems/table_print-1.5.6/lib/table_print/config_resolver.rb:66:in `option_to_column': undefined method `merge' for [:apple, :water, :orange]:Array
возможное решение, но дорогое
я мог бы написать что-то вроде этого, но
def nicePrintToFile(f)
i=0
product = Struct.new(:words,:occurances)
products=Array.new(self.hash.size)
self.hash.keys.sort.each do |key|
products[i+=1]=product.new(key, self.hash[key])
end
tp.set :io, f
tp products
end
, поэтому есть ли решение, которое не требует дополнительных данных, потому что это просто прототип, и я планирую работать с большими файлами.