@anonymous/

PeachpuffIckyDiamondbackrattlesnake

Ruby

No description

fork
loading
Files
  • main.rb

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8965216866024692","path":"main.rb","file":{"path":"main.rb","content":{"asEncoding":{"base64":"IyBVc2Ugb2JqZWN0LW9yaWVudGVkIFJ1YnkgdG8gbW9kZWwgYSBwdWJsaWMgbGlicmFyeQojICh3LyB0aHJlZSBjbGFzc2VzOiBMaWJyYXJ5LCBTaGVsZiwgJiBCb29rKS4gKgojIFRoZSBsaWJyYXJ5IHNob3VsZCBiZSBhd2FyZSBvZiBhIG51bWJlciBvZiBkaXN0aW5jdCBzaGVsdmVzLiAtLUNIRUNLCiMgRWFjaCBzaGVsZiBzaG91bGQga25vdyB3aGF0IGJvb2tzIGl0IGNvbnRhaW5zLiAtLUNIRUNLCiNDcmVhdGUgbWV0aG9kcyB0byBhZGQgYW5kIHJlbW92ZSBhIGJvb2sgZnJvbSBhIHNlbGYuIC0tQ0hFQ0sKI1RoZSBsaWJyYXJ5IHNob3VsZCBoYXZlIGEgbWV0aG9kIHRvIHJlcG9ydCBhbGwgYm9va3MgaXQgY29udGFpbnMuIC0tQ0hFQ0sKI05vdGU6IHRoaXMgc2hvdWxkICpub3QqIGJlIGEgUmFpbHMgYXBwIC0ganVzdCBhIHNpbmdsZSBmaWxlIHdpdGggdGhyZWUgY2xhc3NlcwojIChwbHVzIGNvbW1hbmRzIGF0IHRoZSBib3R0b20gc2hvd2luZyBpdCB3b3JrcykgaXMgYWxsIHRoYXQgaXMgbmVlZGVkLgoKY2xhc3MgTGlicmFyeQogIGRlZiBpbml0aWFsaXplCiAgZW5kCgogIGRlZiBsaXN0X3NoZWx2ZXMKICAgIHB1dHMgIkxpYnJhcnkgY29udGFpbnMgI3tTaGVsZi5udW1iZXJfb2ZfaW5zdGFuY2VzfSBzaGVsdmVzOiIKICAgIHB1dHMgIiN7U2hlbGYuc2hlbGZfbGlzdH0iCiAgZW5kCgogIGRl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
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Use object-oriented Ruby to model a public library
# (w/ three classes: Library, Shelf, & Book). *
# The library should be aware of a number of distinct shelves. --CHECK
# Each shelf should know what books it contains. --CHECK
#Create methods to add and remove a book from a self. --CHECK
#The library should have a method to report all books it contains. --CHECK
#Note: this should *not* be a Rails app - just a single file with three classes
# (plus commands at the bottom showing it works) is all that is needed.

class Library
  def initialize
  end

  def list_shelves
    puts "Library contains #{Shelf.number_of_instances} shelves:"
    puts "#{Shelf.shelf_list}"
  end

  def list_all_books
    puts "Library contains #{Book.number_of_instances} books:"
    puts "#{Book.book_list}"
  end
end

class Shelf
  attr_accessor :contents, :name
  @@shelf_count = 0
  @@shelf_list = []

  def initialize(name)
    @name = name
    @@shelf_count += 1
    @@shelf_list << @name
    @contents = []
    puts "created new shelf #{name}"
  end

  def list_books
    # book_string = @contents.map {|book| puts book.to_s}
    # how do I make this return put just the book title
    # and not all the other metadata? ex:
    # "[#<Book:0x007f85ab021470 @name="Alice in Wonderland">]"
    puts "#{name} shelf contains #{@contents}."
  end

  def shelve(book_name)
    @contents << book_name.name
    puts "#{book_name.name} added to shelf #{self.name}"
    # add a shelf attribute to the Book instance?

  end

  def unshelve(book_name)
    @contents.delete(book_name.name)
    # remove the shelf attribute from the Book instance?
  end

  def self.number_of_instances
    return @@shelf_count
  end

  def self.shelf_list
    return @@shelf_list
  end
end

class Book
  attr_accessor :name, :shelf
  @@book_count = 0
  @@book_list = []

  def initialize(name)
    @name = name
    @@book_count += 1
    @@book_list << @name
    @shelf = nil
    puts "added #{name} to the library"
  end

  def self.number_of_instances
    return @@book_count
  end

  def self.book_list
    return @@book_list
  end

  def remove
    # how to delete an instance of a class? could not find online
    # self.delete
    @@book_list.delete(@name)
    @@book_count -= 1
    # if @shelf not nil
      # find out what shelf it's on and then unshelve it from that shelf instance
      # Shelf.unshelve(@name)
    # make it so a book must be unshelved before being deleted?
  end

  def is_shelved?
    # how to tell if a book is currently on a shelf or not?
    # return true or false
  end
end


my_library = Library.new

alice = Book.new("Alice in Wonderland")
wizard = Book.new("The Wonderful Wizard of Oz")
fiction = Shelf.new("fiction")

fiction.shelve(alice)
fiction.list_books
fiction.shelve(wizard)
non_fiction = Shelf.new("non-fiction")

how_to_influence_people = Book.new("How to Make Friends and Influence People")

non_fiction.shelve(how_to_influence_people)
puts fiction.list_books
puts non_fiction.list_books

alice.remove
fiction.unshelve(alice)
puts fiction.list_books

puts ".... ... ..."

my_library.list_shelves
my_library.list_all_books
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]