Skip to content

Instantly share code, notes, and snippets.

@qrohlf
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qrohlf/68ea125caf2b57eeda19 to your computer and use it in GitHub Desktop.
Save qrohlf/68ea125caf2b57eeda19 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Read the file into a string
file_contents = File.read('memes.txt')
# Split those lines into an array
lines = file_contents.split("\n")
# You could also do the same thing in one step with File.readlines,
# but note that File.readlines will keep newlines whereas split strips them
unless File.readlines('memes.txt') == lines
puts "readlines works, too - but it keeps newlines so it's a bit different!"
end
# Let's grab the title using Array's shift method, which will remove it from the array
# This is also a good time to try out ruby string interpolation (i.e. the #{} stuff)
puts "title: #{lines.shift}"
# Okay, now we can iterate through the lines of the file
lines.each do |line|
# You can totally assign multiple values at once from an array return value in Ruby!
year, meme = line.split("-")
# and here we have some more string interpolation
puts "meme for #{year}: #{meme}"
end
Top Memes by Year
2014 - What Does The Fox Say
2013 - Harlem Shake
2012 - Gangnam Style
2011 - Friday
2010 - Old Spice Guy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment