の 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
stack.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