Skip to content

@eregon /cycle_tracks.rb secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
def skip_tracks_magnets(arr, i)
# Sorry, got no idea how to do with the magnets
# I probably lost the ability to code that way in Ruby ;)
end
def skip_tracks_ruby playlist, tracks
return if tracks.zero?
size = playlist.size
if tracks >= 0
tracks %= size
playlist[0...size-tracks], playlist[size-tracks..-1] = playlist[tracks..-1], playlist[0...tracks]
else
tracks = tracks.abs % size
playlist[0...tracks], playlist[tracks..-1] = playlist[size-tracks..-1], playlist[0...size-tracks]
end
end
alias :skip_tracks :skip_tracks_ruby
require "./cycle_tracks"
describe "Cycle Tracks" do
let(:playlist) {
Array.new(10) { |i| "song #{i+1}" }
}
let(:size) { playlist.size }
let(:original_playlist) { playlist.dup.freeze }
it "should rest with zero" do
playlist.freeze
lambda { skip_tracks(playlist, 0) }.should_not raise_error(RuntimeError, "can't modify frozen array")
playlist.should == original_playlist
end
it "should be able to skip tracks ahead" do
(1..size-1).each { |i|
playlist = original_playlist.dup
skip_tracks(playlist, i)
playlist.should == original_playlist[i..-1] + original_playlist[0...i]
}
end
it "should be able to take negative numbers" do
(1-size..-1).each { |i|
playlist = original_playlist.dup
skip_tracks(playlist, i)
playlist[0...-i].should == original_playlist[size+i..-1]
playlist[-i..-1].should == original_playlist[0...size+i]
}
end
it "should be able to do this with any number" do
(-2*size..2*size).each { |i|
playlist = original_playlist.dup
skip_tracks(playlist, i)
if i < 0
i = i % size - size
playlist[0...-i].should == original_playlist[size+i..-1]
playlist[-i..-1].should == original_playlist[0...size+i]
elsif i == 0
playlist.should == original_playlist
else
i %= size
playlist[0...size-i].should == original_playlist[i..-1]
playlist[size-i..-1].should == original_playlist[0...i]
end
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.