Skip to content

Instantly share code, notes, and snippets.

@skanev skanev/collection.rb Secret

Created Oct 29, 2012
Embed
What would you like to do?
Assorted code writen during a Ruby lecture
class Song
attr_reader :name, :artist, :genre, :subgenre, :tags
def initialize(name, artist, genre, subgenre, tags)
@name = name
@artist = artist
@genre = genre
@subgenre = subgenre
@tags = tags
end
end
class Collection
def initialize(songs)
@songs = songs
end
def first
@songs.first
end
def self.parse(input)
songs = input.lines.map do |line|
name, artist, genre_and_subgenre, tags_as_string = line.split('.').map(&:strip)
genre, subgenre = genre_and_subgenre.split(',').map(&:strip)
tags = if tags_as_string.nil?
[]
else
tags_as_string.split(',').map(&:strip)
end
Song.new name, artist, genre, subgenre, tags
end
new songs
end
end
require 'minitest/unit'
require './collection'
INPUT = <<END
My Favourite Things. John Coltrane. Jazz, Bebop. popular, cover
Greensleves. John Coltrane. Jazz, Bebop. popular, cover
Alabama. John Coltrane. Jazz, Avantgarde. melancholic
Acknowledgement. John Coltrane. Jazz, Avantgarde
Afro Blue. John Coltrane. Jazz. melancholic
'Round Midnight. John Coltrane. Jazz
My Funny Valentine. Miles Davis. Jazz. popular
Tutu. Miles Davis. Jazz, Fusion. weird, cool
Miles Runs the Voodoo Down. Miles Davis. Jazz, Fusion. weird
Boplicity. Miles Davis. Jazz, Bebop
Autumn Leaves. Bill Evans. Jazz. popular
Waltz for Debbie. Bill Evans. Jazz
'Round Midnight. Thelonious Monk. Jazz, Bebop
Ruby, My Dear. Thelonious Monk. Jazz. saxophone
Fur Elise. Beethoven. Classical. popular
Moonlight Sonata. Beethoven. Classical. popular
Pathetique. Beethoven. Classical
Toccata e Fuga. Bach. Classical, Baroque. popular
Goldberg Variations. Bach. Classical, Baroque
Eine Kleine Nachtmusik. Mozart. Classical. popular, violin
END
class CollectionTest < MiniTest::Unit::TestCase
# My Favourite Things. John Coltrane. Jazz, Bebop. popular, cover
def test_parsing_a_collection_file
collection = Collection.parse(INPUT)
song = collection.first
assert_equal 'My Favourite Things', song.name
assert_equal 'John Coltrane', song.artist
assert_equal 'Jazz', song.genre
assert_equal 'Bebop', song.subgenre
assert_equal %w[popular cover], song.tags
end
def test_tags_is_an_empty_array_if_not_specified
input = 'Acknowledgement. John Coltrane. Jazz, Avantgarde'
collection = Collection.parse(input)
song = collection.first
assert_equal song.tags, []
end
end
require './tests'
class Rat
attr_reader :num, :den
def initialize(num, den)
gcd = num.gcd den
@num = num / gcd
@den = den / gcd
end
def ==(other)
num == other.num and den == other.den
end
def +(other)
Rat.new num * other.den + other.num * den, den * other.den
end
def -(other)
self + -other
end
def -@
Rat.new(-num, den)
end
end
ok Rat.new(1, 2).num == 1
ok Rat.new(1, 2).den == 2
ok Rat.new(1, 2) == Rat.new(1, 2)
ok Rat.new(3, 2) != Rat.new(1, 2)
ok Rat.new(1, 3) != Rat.new(1, 2)
ok Rat.new(1, 2) == Rat.new(2, 4)
ok Rat.new(15, 20) == Rat.new(9, 12)
ok Rat.new(1, 2) + Rat.new(3, 4) == Rat.new(5, 4)
ok Rat.new(1, 2) + Rat.new(1, 2) == Rat.new(1, 1)
ok Rat.new(3, 4) - Rat.new(1, 2) == Rat.new(1, 4)
ok(-Rat.new(3, 4) == Rat.new(-3, 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.