How is mutation done in Ruby for this:
row = row.to_hash
I’ve tried row.to_hash!
But it doesn’t work.
EDIT: Here is the code where it’s in:
CSV.foreach('stores.csv', :headers => true) do |row|
row = row.to_hash
end
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Unless I misunderstand your question you might try:
It’s not actually mutation, but it does give you the output you seem to be looking for. One of the reasons for having separate methods for performing an action for new output vs. changing data in place is that in general it is wiser to have your input data be immutable. Reading from a CSV file probably doesn’t fit into this paradigm cleanly (the original input is in a file somewhere), which is likely why there’s no ! version of to_hash.
Optionally in my above code you could just process the hash and put the results where you want it if the input file is humongous in order to save space, rather than just shoving all the hashes into the array and then processing that array.