Last active
August 29, 2015 14:02
-
-
Save qrohlf/68ea125caf2b57eeda19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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