Ruby 动态创建类时初始化出错
我在命令行收到一些参数。第一个命令告诉我必须创建什么类型的uby对象才能执行所需的操作。我将此参数存储在Ruby 动态创建类时初始化出错,ruby,class,methods,initialization,instance,Ruby,Class,Methods,Initialization,Instance,我在命令行收到一些参数。第一个命令告诉我必须创建什么类型的uby对象才能执行所需的操作。我将此参数存储在@entity中,然后通过 entity_class = "EmeraldFW::#{@entity.capitalize}".split('::').inject(Object) {|o,c| o.const_get c} entity_instance = entity.new(@arguments,@options) entity_instance.execute_command 当我
@entity
中,然后通过
entity_class = "EmeraldFW::#{@entity.capitalize}".split('::').inject(Object) {|o,c| o.const_get c}
entity_instance = entity.new(@arguments,@options)
entity_instance.execute_command
当我试图创建其中一个实例(比如Project)时,我遇到了一个错误
我的专题课是
module EmeraldFW
class Project < EmeraldFW::Entity
def self.initialize(args,opts)
@valid_option = [ :language, :test, :database, :archetype ]
super(args,opts)
end
.
.
.
我的错误是
/home/edvaldo/software/github/emeraldfw21/lib/emeraldfw.rb:41:in `initialize': wrong number of arguments (given 2, expected 0) (ArgumentError)
我不知道为什么会这样。正如您所看到的,initialize接收两个参数,我根据需要给它两个参数
也许是因为我已经看了很长时间了,但我就是看不出原因。有人能帮我吗?这是因为您的
初始化
方法被编写为“类”方法(类的单例方法),而它应该是一个实例方法。由于这一事实,您正在使用new
调用的原始初始化方法:
entity_instance = entity.new(@arguments,@options)
不需要争论
要解决此问题,请从self.initialize
方法定义中删除self.
部分
class-Foo
def初始化(巴,巴)
@巴
@baz=baz
结束
结束
Foo.new(:bar,:baz)
#=> #
entity_instance = entity.new(@arguments,@options)
class Foo
def initialize(bar, baz)
@bar = bar
@baz = baz
end
end
Foo.new(:bar, :baz)
#=> #<Foo:0x007fa6d23289a0 @bar=:bar, @baz=:baz>