Skip to content

Instantly share code, notes, and snippets.

@katiesmillie
Last active August 29, 2015 14:13
Show Gist options
  • Save katiesmillie/cbbe037ba09a8bf75a6b to your computer and use it in GitHub Desktop.
Save katiesmillie/cbbe037ba09a8bf75a6b to your computer and use it in GitHub Desktop.
Code for Hacker School Application. I had never written anything from scratch, so I decided to tackle something that mimicked the basic functionality one of my side projects, a Rails app called dearfuture.me.
#!/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~ ~ ~"
@austenito
Copy link

Hey Katie - Some comments:

  • You might want to clean some of the white space and weird indentation in the code. It will make your code lot cleaner. A standard number of indentation for code is 2 spaces.
#  There is a space between '(' and 'NoteStruct'. Also remove the trailing comma
note_struct = Struct::new( "NoteStruct", :message_date , :message , :days_ago,) 
  • You can refactor:
if File.exist?("notes.yaml")
else
    File.new "notes.yaml", "w"
end

to

unless File.exist?("notes.yaml")
  File.new "notes.yaml", "w"
end
  • Try not to use single letter variables

I think you want n to be 'note'

YAML.load_documents(file).each_with_index do |n, index|
  • Not sure what you are trying to do here:

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?

    date = n.message_date
    if Date.parse(date) <= DateTime.now
        puts "
        You have an unread message from #{n.days_ago} days ago: 
        #{n.message}"
    elsif index == 0
        File.open('notes.yaml', 'w') {|file| file.puts n.to_yaml } 
    else
        File.open('notes.yaml', 'a') {|file| file.puts n.to_yaml } 
    end
  • One other thing to think about is what happens when the user enters bad input? What if I don't enter in the number of days? Negative days? I don't enter a note in?

@katiesmillie
Copy link
Author

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"?

@austenito
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment