ArgumentError`initialize':参数个数错误(0表示0)

时间:2013-08-13 19:02:48

标签: ruby constructor arguments

所以我对Ruby很新,我正在尝试通过将我正在研究的Python脚本转换为Ruby来学习更多内容。但是,我遇到了一个非常烦人的问题。每次我尝试运行以下代码时,都会收到此错误:

  

player.rb:81:来自player.rb的initialize': wrong number of arguments(8 for 0) (ArgumentError) from player.rb:81:in new':81:in   `'

我很肯定初始化方法中有8个参数,我已多次检查拼写。我似乎无法弄清楚为什么解释器不相信构造函数没有任何参数。有人可以告诉我下面出了什么问题吗?

class Player
    def determineLevel
        @xp_req4lvl = [0, 1000, 2250, 3750, 5500, 7500, 10000, 13000, 16500, 20500, 26000, 32000, 39000,47000,57000,69000,83000,99000,119000,143000,175000,210000,255000,310000,375000,450000,550000,675000,825000,1000000]
        if xp <= 1000 then
            @level = 1
            return
        end
        i=0
        while i < xp_req4lvl.length do
            if xp > xp_req4lvl[i] then
                i+=1
            elsif xp == xp_req4lvl[i] then
                @level = i+1
                return
            else
                @level = i
                return
        end
        ml = xp_req4lvl.length
        raise LevelingError.new(ml), "Level too high!", caller
        return
    end

    def initialize(personalInfo, training, race, chclass, feats, stuff, abilities, xp)
        @str, @con, @dex, @int, @wis, @cha = abilities
        @trainedSkills = training
        @characterClass = chclass
        @feats = feats
        @race = race
        #for featx in self.race["racialFeats"]:
        #    self.feats.append(featx)
        @equipment = stuff
        @background = personalInfo
        @xp =xp
        @actionPoints = 1
        @equippedArmor = nil
        @equippedShield = nil
        @armorProficiencies = []
        @shieldProficiencies = []
        @untrainedArmorEquipped = false
        @untrainedShieldEquipped = false
        @level = 0
        self.determineLevel
        rescue LevelingError => e
            puts "Character is over maximum level of " + e.get_max_lvl.to_s
            @level = 30
        end
        #self.calculateAbilityScores
        #self.calculateAbilityMods
        #self.calculateHP
        #self.determineProficiencies

        #@fortitudeSave = 10+(@level/2)
        #@reflexSave = 10+(@level/2)
        #@willSave = 10+(@level/2)
        #puts @level.to_s

        @healingSurgesUsed = 0
    end

    public
    def get_level
        return @level
    end      
end

class LevelingError < RuntimeError
    attr :maxLvl

    def initialize(ml)
        @maxLvl = ml
    end

    def get_max_lvl()
        return @maxLvl
    end
end

if __FILE__ == $0
    j = Player.new("0", "1", "2", "3", "4", "5", "[10,10,10,10,10,10]", "1250")
    puts j.get_level.to_s
end

1 个答案:

答案 0 :(得分:2)

您错过了一个end关键字:

while i < xp_req4lvl.length do
            if xp > xp_req4lvl[i] then
                i+=1
            elsif xp == xp_req4lvl[i] then
                @level = i+1
                return
            else
                @level = i
                return
        end # !> mismatched indentations at 'end' with 'while'

如果您在此处再添加一个end,则不会收到错误。但是由于以下原因,您将获得另一个syntax error, unexpected keyword_end, expecting end-of-input

def initialize(personalInfo, training, race, chclass, feats, stuff, abilities, xp)
        @str, @con, @dex, @int, @wis, @cha = abilities
        @trainedSkills = training
        @characterClass = chclass
        @feats = feats
        @race = race
        #for featx in self.race["racialFeats"]:
        #    self.feats.append(featx)
        @equipment = stuff
        @background = personalInfo
        @xp =xp
        @actionPoints = 1
        @equippedArmor = nil
        @equippedShield = nil
        @armorProficiencies = []
        @shieldProficiencies = []
        @untrainedArmorEquipped = false
        @untrainedShieldEquipped = false
        @level = 0
        self.determineLevel
        rescue LevelingError => e
            puts "Character is over maximum level of " + e.get_max_lvl.to_s
            @level = 30
        end # !> mismatched indentations at 'end' with 'def'

您需要遵循以下内容:

def method_name(..)
  begin
   # your code
  rescue
   # your codee
  end
end

但请勿在{{1​​}}方法中使用begin..rescue