Skip to content

Instantly share code, notes, and snippets.

@jasdeepsingh
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdeepsingh/1d71f2cbe51754a3f885 to your computer and use it in GitHub Desktop.
Save jasdeepsingh/1d71f2cbe51754a3f885 to your computer and use it in GitHub Desktop.
Recent Files Tracker

Let's build a Recent File Tracker

  • Implement a Class, name it RecentFiles

  • You can start implementing your class from 2nd line onwards.

  • You can run the test code, which will test your code using the following command: rspec file_name.rb

  • You can replace the file_name.rb with your actual file which you are working with.

  • If you get any errors such as rspec not found etc, please do: sudo gem install rspec

  • The class should be able to handle the following scenarios:

    • Output a list of most recent accessed files.
    • If a file with same name is accessed multiple times, your list should include or consider it only once.
    • Your list should keep track of only most recent 5 files.
    • The most recent file should be on top of the list OR first element in your list
    • Here's how the code API should look like:
class RecentFiles
 # your code here
end

recent = RecentFiles.new([])
or
recent = RecentFiles.new(['file1', 'file2'])

recent.add('filename') # this file should go to the top of the list

recent.files # this should give you ['filename', 'file1', 'file2']
require 'rspec'
describe RecentFiles do
describe '#initialize' do
it 'can be initialized with an empty list' do
recent = RecentFiles.new([])
expect(recent.files)to eq([])
end
it 'cannot be initialized without a list of file names' do
expect { recent = RecentFiles.new }.to raise_error
end
it 'can be initialized with less than 5 files' do
file_list = [1,2,3,4]
recent = RecentFiles.new(file_list)
expect(recent.files).to eq(file_list)
end
it 'can contain maximum 5 files only/1st five' do
file_list = [1,2,3,4,5,6]
recent = RecentFiles.new(file_list)
expect(recent.files).to eq(file_list[0..4])
end
end
describe '#list' do
it 'should return the list of the files' do
file_list = ['file1', 'file2', 'file3']
recent = RecentFiles.new(file_list)
expect(recent.files).to eq(file_list)
end
it 'should return the running list' do
recent = RecentFiles.new(['file1', 'file2', 'file3'])
recent.add('file4')
recent.add('file5')
expect(recent.files).to eq(['file5', 'file4', 'file1', 'file2', 'file3'])
end
end
describe '#add' do
it 'should be able to add files to the list' do
recent = RecentFiles.new([])
recent.add('filename')
expect(recent.files).to include('filename')
end
it 'cannot contain duplicates' do
recent = RecentFiles.new([])
recent.add('filename')
recent.add('filename')
expect(recent.files).to eq(['filename'])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment