secret
Created

  • Download Gist
cycle_tracks.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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
cycle_tracks_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.