명령 줄에서 몇 가지 매개 변수를받습니다. 첫 번째는 원하는 작업을 수행하기 위해 어떤 종류의 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
이러한 인스턴스 중 하나를 만들려고 할 때 오류가 발생합니다 (예 : Project).
내 프로젝트 클래스는
module EmeraldFW
class Project < EmeraldFW::Entity
def self.initialize(args,opts)
@valid_option = [ :language, :test, :database, :archetype ]
super(args,opts)
end
.
.
.
내 클래스 Entity는
module EmeraldFW
class Entity
attr_accessor :entity_type, :valid_commands
def self.initialize(args,opts)
@args = args
@opts = clean_option(opts)
end
.
.
.
내 오류는
/home/edvaldo/software/github/emeraldfw21/lib/emeraldfw.rb:41:in `initialize': wrong number of arguments (given 2, expected 0) (ArgumentError)
왜 이런 일이 일어나는지 모르겠습니다. 보시다시피 initialize는 두 개의 인수를 수신하고 필요에 따라 두 개의 인수를 제공했습니다.
이걸 오랫동안 쳐다 보니까 이유를 알 수가 없어요. 누군가 나를 도와 줄까요?
그것은 당신의 initialize
메서드가 "클래스"메서드 (class 'singleton 메서드)로 작성되었지만 인스턴스 메서드 여야하기 때문입니다. 그 사실 때문에 initialize
당신이 호출하는 원래 방법 new
:
entity_instance = entity.new(@arguments,@options)
인수를 취하지 않습니다.
문제를 해결하려면 메서드 정의 에서 부품을 제거하십시오 .self.
self.initialize
class Foo
def initialize(bar, baz)
@bar = bar
@baz = baz
end
end
Foo.new(:bar, :baz)
#=> #<Foo:0x007fa6d23289a0 @bar=:bar, @baz=:baz>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다