Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All tests passed for both files #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/Queue.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
class Queue
attr_accessor :store

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not want to do this. This makes @store available outside of the Queue class which means that someone could do something like:

q = Queue.new
# ...stuff here
q.store[2] = 100

So basically it undoes the limitations that we put on accessing/modifying elements in a Queue by letting you directly modify the Queue.

To solve this, just remove this line attr_accessor :store
We similarly wouldn't want it to be attr_reader either

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahhh okay, I think I understand why an attr_accessor is not a good idea but I'm still a bit fuzzy on why attr_reader Isn't a good idea. If I wanted to see the queue contents in literal form, instead of calling ruby q.store # :store in attr_reader I would just do something like ruby puts q ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully class clarified this. If it didn't, let's talk.


def initialize
@store = Array.new
end

def enqueue(element)
@store.unshift << element
end

def dequeue
@store.shift
end

def front
return @store.first
end

def size
return @store.length
end

def empty?
if @store.length < 1
return true
else
return false
end
end

def to_s
return @store.to_s
end
end

# q = Queue.new
# puts q.size
# puts q.empty?
9 changes: 9 additions & 0 deletions lib/Stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ def initialize
end

def push(element)
@store.push(element)
end

def pop
# last_element = @store.last
# @store.delete(@store.last)
return @store.pop
end

def top
@store.last
end

def size
return @store.length
end

def empty?
#return @store.length == 0
return size == 0
#return @store.empty?
end

def to_s
Expand Down