-
-
Save katiesmillie/cbbe037ba09a8bf75a6b to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby | |
require 'yaml' | |
require 'date' | |
unless File.exist?("notes.yaml") | |
File.new "notes.yaml", "w" | |
end | |
note_struct = Struct::new("NoteStruct", :message_date, :message, :days_ago) | |
file = File.open "notes.yaml", "r" | |
YAML.load_documents(file).each_with_index do |note, index| | |
date = note.message_date | |
if Date.parse(date) <= DateTime.now | |
puts " | |
You have an unread message from #{note.days_ago} days ago: | |
#{note.message}" | |
elsif index == 0 | |
File.open('notes.yaml', 'w') {|file| file.puts note.to_yaml } | |
else | |
File.open('notes.yaml', 'a') {|file| file.puts note.to_yaml } | |
end | |
end | |
puts "\nTime to fire up the flux capacitor and send a note to your future self. | |
How many days into the future would you like to send your note?" | |
days = gets.to_i | |
i = Time.now.to_i | |
message_time = (days*86400) + i | |
message_date = DateTime.strptime("#{message_time}",'%s').strftime("%B %d, %Y") | |
puts "Type your message and return to send." | |
message = gets.chomp | |
note = note_struct.new "#{message_date}", "#{message}", "#{days}" | |
file = File.open "notes.yaml", "a" | |
file.puts note.to_yaml | |
puts "~ ~ ~ \n Great Scott! Your message is now traveling #{days} days to the future.\n~ ~ ~" |
Austen - thanks, super helpful.
I did a pass at indenting and whitespace clean up, also replaced hard line breaks in my strings with /n ... is that kosher? Does this look better overall? Do I need to take out more whitespace, not 100% sure where to create breaks.
Re: the logic of treating the first item in my array different, I agree it's weird. :) Originally I included a true/false variable to indicate whether a note had been seen by the user, but I was having trouble updating a single key value back into the YAML file, so I decided to just blow away the note once it was read. But that turned out to be tricky as well, so what I came up with is: 1) read out all the notes that are "due" 2) rewrite the remaining notes back to the file. If I just append, then duplicates get created and if I just write then it keeps rewriting over each struct through the iteration and I was just left with one struct. Therefore, this solution rewrites the entire file with the first struct, then appends all the remaining structs. I was thinking that coming up with a more elegant solution, or trying to update a flag is something that I could work on in my pairing interview if I get one. But if you have any thoughts I would love to know.
Re: bad user input, if the user puts in an non-integer for date it just goes to 0 which I'm ok with for now, negative days are accepted and you'll get the note next time you run the program. Anything, including nil, is accepted for a note. So far this hasn't cause any errors, but I see how it would be cleaner to validate it. How you would validate the presence of "anything"?
Does this look better overall? Do I need to take out more whitespace, not 100% sure where to create
breaks.
It looks much better!
Re: the logic of treating the first item in my array different, I agree it's weird.
You had the right idea of trying to update a 'read' key in the yaml file. YAML is a set of keys and values so you should be able to overwrite/create new keys. Check out: http://stackoverflow.com/questions/14532959/how-do-you-save-values-into-a-yaml-file
The thought process around YAML files is:
- Read YAML file into a ruby object (probably a hash of some sort)
- Mutate the hash. Update it's values, remove keys, add keys.
- Dump the new YAML file into the existing YAML file.
Ruby will overwrite keys with new values, remove keys that don't exist, and create new keys.
I do think it would be a good thing to pair on. If anything, it's something interesting to talk about during your pairing interview.
How you would validate the presence of "anything"?
You could count the number of characters in the user input and disallow empty messages.
Hey Katie - Some comments:
to
I think you want n to be 'note'
This is kind of weird. It seems like if the first note is not for today, then write itself back to the file. Also every other note gets appended to the file?