There is a file with the text: [#<Task name: "John", date: "2015-07-23">, #<Task name: "Devid", date: "2014-07-21">, #<Task name: "John", date: "2013-01-23">]

It is necessary to split the file into an array of hashes. Ultimately, you need to be able to remove any hash, for example, delete all hashes with name: "John".

  • But this is not hashes. Do you generate such a file yourself? - D-side
  • You form this file incorrectly - you need to use YAML or Marshal, and not file.puts task.inspect - Nakilon

1 answer 1

To be honest, it only comes to my mind to parse the contents of the file and recreate an array of hashes

 str = IO.read('file.txt') arr = [] str.scan /#<Task([^>]+)>/ do |match| name, date = match[0].strip.split(','); name_key, name_value = name.split(':') date_key, date_value = date.split(':') h = {} h[name_key] = name_value.gsub(/[\"]+/, '').strip h[date_key.strip] = date_value.gsub(/[\"]+/, '').strip arr << h end p arr