Last active
December 25, 2015 15:49
-
-
Save irmiller22/7001434 to your computer and use it in GitHub Desktop.
serialize.rb
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
class Song | |
attr_accessor :title, :artist | |
def serialize | |
file_name = self.title.gsub(" ", "_").downcase | |
File.open("#{file_name}.txt", "w+") do |f| | |
f << "#{self.artist.name} - #{self.title}" | |
end | |
end | |
def deserialize(filename) | |
File.open(filename, "r") do |text| | |
line = IO.readlines(text).first | |
artist, song_title = line.split(" - ") | |
Song.new(song_title).tap {|s| s.artist = Artist.new(artist)} | |
end | |
end | |
end | |
class Artist | |
attr_accessor :name | |
def initialize(artist_name) | |
@name = artist_name | |
end | |
end | |
describe Song do | |
it "has a title" do | |
song = Song.new | |
song.title = "Night Moves" | |
song.title.should eq("Night Moves") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment