【问题标题】:ArgumentError `initialize': wrong number of arguments(8 for 0)ArgumentError `initialize':参数数量错误(8 代表 0)
【发布时间】:2013-08-13 19:02:48
【问题描述】:

所以我对 Ruby 很陌生,我正在尝试通过将我正在处理的 python 脚本转换为 Ruby 来了解更多信息。但是,我遇到了一个非常烦人的问题。每次我尝试运行以下代码时,都会收到此错误:

player.rb:81:in initialize': wrong number of arguments(8 for 0) (ArgumentError) from player.rb:81:innew' 来自 player.rb: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

【问题讨论】:

    标签: ruby constructor arguments


    【解决方案1】:

    您在这里错过了一个 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
    

    但不要在#initialize 方法中使用begin..rescue

    【讨论】:

    • 试试我说的......他错过了while结束,所以#initialize方法进入了while循环。如果你在那里放一个end,你不会得到参数错误。先尝试,然后否决正确答案..
    • 好吧,我的错,给你一个。没有真正得到你的答案,现在我知道了。
    • @BeatRichartz 感谢您的出现... :) 我尝试了代码然后回答了..但是当我按下 ctrl+X.. 时,我的编辑器清楚地标记了错误点: )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多