我正在构建一个函数来构建一个操作ruby中的链表。现在我正在研究delete
方法并试图在rspec测试中包含puts语句,以确认我在编程时的假设。
我希望显示的变量是@head
,examined_node
,examined_node.next
@head.next
,after_examined_node
,@tail
。真的,我觉得如果有人可以帮助解释或指点我如何提取其他人应该遵循的好文章。
这是我的spec文件中的特定块,它失败了一些我试图用来获得反馈的代码。我应该注意到我一个接一个地尝试了这些但是在这里取消了它们。
it "removes the head of a list properly" do
llist.delete(n1)
puts @head
puts self.examined_node.data
puts examined_node
puts @tail
expect(llist.head).to eq n2 #Breaks here
expect(llist.head.next).to eq n3
expect(llist.tail).to eq n3
end
这是我的node.rb
文件
class Node
attr_accessor :next
attr_accessor :data
def initialize(data)
@data = data
@next = nil
end
end
以下是我的linked_list.rb
文件的相关部分
require_relative 'node'
class LinkedList
attr_accessor :head
attr_accessor :tail
attr_accessor :current
attr_accessor :bob
def initialize(*args)
@current = nil
@bob = []
end
# This method creates a new `Node` using `data`, and inserts it at the end of the list.
def add_to_tail(node)
if(@tail)
@tail.next = node
@tail = node
else
@head = node
@tail= node
end
end
def delete(node)
examined_node = @head
after_examined_node = @head.next
end
这是我的Rspec
文件
include RSpec
require_relative 'node'
require_relative 'linked_list'
RSpec.describe LinkedList, type: Class do
let(:n1) { Node.new("Rob") }
let(:n2) { Node.new("Ben") }
let(:n3) { Node.new("Mike") }
let(:llist) { LinkedList.new }
describe "#delete" do
before do
llist.add_to_tail(n1)
llist.add_to_tail(n2)
llist.add_to_tail(n3)
end
it "removes the head of a list properly" do
llist.delete(n1)
puts @head
puts self.examined_node.data
puts examined_node
puts @tail
expect(llist.head).to eq n2 #Breaks here
expect(llist.head.next).to eq n3
expect(llist.tail).to eq n3
end
it "removes the middle element of a list properly" do
llist.delete(n2)
expect(llist.head).to eq n1
expect(llist.head.next).to eq n3
expect(llist.tail).to eq n3
end
it "removes the last element of a list properly" do
llist.delete(n3)
expect(llist.head).to eq n1
expect(llist.head.next).to eq n2
expect(llist.tail).to eq n2
end
end