我试图动态地要求然后在初始化方法中包含模块。
# file: object.rb
class Object
attr_accessor :array
def initialize array
@array = array
@array.each do |f|
require_relative "#{f}"
include f.capitalize # the method name is the same as the filename
puts @variable # property from included method
end
end
end
object = Object.new ['a','b','c']
使用这些模块文件
# file: a.rb
module A
@variable = 'string A'
end
等等 b 和 c
我不断收到错误消息:
`block in initialize': undefined method `include'
我在这里做错了什么,有没有更好的方法来实现我想要做的事情?