为什么我已经在使用 include 方法时需要 require "stacklike"
in ?如果我删除,它会产生错误。stack.rb
stacklike.rb
require
"Uninitialized constant Stack::Stacklike (NameError)"
stacklike.rb
module Stacklike
def stack
@stack ||= [] #@stack || @stack = []
end
def add_to_stack(obj)
stack.push(obj)
end
def take_from_stack
stack.pop
end
end
堆栈.rb
require_relative "stacklike"
class Stack
include Stacklike
end
s = Stack.new
s.add_to_stack("people")
s.add_to_stack("people2")
s.add_to_stack("people3")
puts "obj currently on the stack:"
puts s.stack
taken = s.take_from_stack
puts "Removed this stack:"
puts taken
puts "Now on stack:"
puts s.stack