所以我对 Ruby 很陌生,我正在尝试通过将我正在处理的 python 脚本转换为 Ruby 来了解更多信息。但是,我遇到了一个非常烦人的问题。每次我尝试运行以下代码时,都会收到此错误:
player.rb:81:in
initialize': wrong number of arguments(8 for 0) (ArgumentError) from player.rb:81:in
new' from 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