Here is a hash that I save to a file to later be read.
my_hash = {-1 => 20, -2 => 30, -3 => 40}
File.open("my_file.txt", "w") { |f| f.write my_hash }
#how it looks opening the text file
{-1 => 20, -2 => 30, -3 => 40}
When I go to read it, is where my problem is. (following code is separate from top)
my_hash = File.foreach("my_file.txt") { |f| print f }
p my_hash
#=> {-1 => 20, -2 => 30, -3 => 40}nil
that nil
messes up the rest of my code..not sure how to get rid of if. Just for clarity the rest of the code...
back_up_hash = {-1 => 20}
if my_hash.nil?
my_hash = back_up_hash
end
That little nil
always makes my_hash equal to back_up_hash. I need that .nil?
just in case the file is doesn't have the hash, otherwise the problem just gets pushed further down.
I also tried to read (slurp?..it's a small file) the file like this....
my_hash = File.read("my_file.txt") { |f| print f }
p my_hash
=> "{-1 => 20, -2 => 30, -3 => 40}"
# not sure how to get it out of string form...and I have searched for it.