Skip to content

Instantly share code, notes, and snippets.

@irmiller22
Last active December 25, 2015 15:49
Show Gist options
  • Save irmiller22/7001434 to your computer and use it in GitHub Desktop.
Save irmiller22/7001434 to your computer and use it in GitHub Desktop.
serialize.rb
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