How do I print the values of hashsets that are saved in a file in ruby -
i have file (named ter.txt
) contains hashsets content below:
{"qty"=>"gfg", "unit"=>"gfg", "item"=>"xcv", "cost"=>"0.0", "salestax"=>"0.0"} {"qty"=>"gdf", "unit"=>"g", "item"=>"gg", "cost"=>"0.0", "salestax"=>"0.0"}
i want print values of hashsets. i've tried following got error of undefined local variable or method 'item'
file = file.open('ter.txt', 'r').map { |line| line.split("\n")[0] } file.each |hash| p hash p " #{hash[item]}" end
you shouldn't save data way. perfect case use yaml. write file like:
--- - qty: gfg unit: gfg item: xcv cost: 0.0 salestax: 0.0 - qty: gdf unit: g item: gg cost: 0.0 salestax: 0.0
then can load with:
require 'yaml' data = yaml.load(file.read 'ter.txt')
Comments
Post a Comment