Week 1 Ruby challenge
The code creates a Library class that contains a list of books read from an
.yml file. The Library includes functions to add new books, checkout and return books.
irbin the terminal and load
- create a new library, add the library
nameas an argument:
library1 = Library.new('library_name')
this will create the file library_name_data.yml in the main folder. And will initiate the library with five basic books.
- The library books can be listed any time using the command:
the result is an array of books, where each book is a hash that contains the book
author. In addition information about the availability of the book
available, the name of person whom checked it out
person and the date it should be returned
- create a new person, add
personnummer (integer)as arguments:
person1 = Person.new(name, personnummer)
the person will have a name, a personnummer and the list of books borrowed form a library. When a new person is created the books are an empty array.
- The person books can be listed any time using the command:
the list shows the books titles, authors and return dates.
- A person can check the list of available books in library using:
this will lest the books available only
- A person can check out a book from library by calling the Library method
.checkoutand passing argument of book
person (object)borrowing the book:
this will set a return date of 30 days from today, and then update the books in the library and the person objects.
- A person can return a book to library in similar method to checkout:
this will update both library and person objects.
- A person can check the return date of unavailable book by ID using the method
- A librarian can add books to the library collection using the method
.add_bookby passing the arguments of the new book
author (string)as follow:
this will automatically create and ID for the book and add it to the collection and write it to the
- Error messages show in cases where a person try return an already returned book, or checkout an unavailable book, etc...
- Please note thats the second book in the initial books
ID = 2is already borrowed
available = falsefor test purpose.