基本的問題,但我不明白爲什麼這個代碼是生產上print_values的「未定義的方法」錯誤...在這個Ruby代碼未定義的方法錯誤鏈表
class LinkedListNode
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node
end
def print_values(list_node)
print "#{list_node.value} --> "
if list_node.next_node.nil?
print "nil\n"
return
else
print_values(list_node.next_node)
end
end
end
node1 = LinkedListNode.new(37)
node2 = LinkedListNode.new(99, node1)
node3 = LinkedListNode.new(12, node2)
print_values(node3)
由於Anuja。顯然,我無法圍繞實例和類方法進行包裝(這裏只是一個初學者)。你可以推薦一些好的解釋來幫助澄清? – amongmany
@amongmany你可以試試這個https://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/113-class-variables –
啊,謝謝。我做了RubyMonk入門,但沒有達到上升。 (有很多地方需要查看基礎知識,這很難找到並堅持一個。)我會檢查一下。 – amongmany