使用哈希作为实例变量

问题描述 投票:-1回答:2

我对Ruby非常陌生。

使用散列作为实例变量时是

class Hero
  def initialize (stats={})

class Hero
  def initialize (stats={'Atk':0, 'Def':0})

class Hero
  def initialize (stats)

或一些更好的方法?我在初始化行上遇到关于意外的':'等的30条错误。

初始化行中可能有10个散列。为每个状态设置一个单独的变量会更好吗?提前致谢!

很抱歉,如果这是重复的,但是我没有看到任何与此相关的问题。

我的班级:

class Hero

  attr_accessor :nam, :desc, :race, :clas, :bg, :heroLVL, :hitpoints, :armC, :spd, :sdcwic, :modifiers, :proficiencies, :skills, :languages, :inventory, :equipment, :coins, :features, :feats, :spellsKnown, :spellSlots, :maxSpellSlots, :currentSpellSlots, :cantrips

  def initialize(nam, desc = {}, race, clas = [], bg, heroLVL = {}, hitpoints = {'Current HP' : 0, 'Max HP' : 0, 'Temp HP' : 0}, armC, spd, sdcwic = {'Stre' : 0, 'Dex' : 0, 'Con' : 0, 'Wis' : 0, 'Int' : 0, 'Cha' : 0}, modifiers = {'Stre' : 0, 'Dex' : 0, 'Con' : 0, 'Wis' : 0, 'Int' : 0, 'Cha' : 0, 'Proficiency' : 0}, proficiencies = [], skills = {}, languages = [], inventory = {}, equipment = {}, coins = {'Copper' : 0, 'Silver' : 0, 'Electrum' : 0, 'Gold' : 0, 'Platinum' : 0}, features = [], feats = [], spellsKnown = [], spellSlots = {}, maxSpellSlots = {}, currentSpellSlots = {}, cantrips = [])
    #description
    @nam = nam
    @desc = desc
    #stats
    @race = race
    @clas = clas 
    @bg = bg
    @heroLVL = heroLVL
    @hitpoints = hitpoints
    @armC = armC
    @spd = spd
    #modifiers
    @sdcwic = sdcwic
    @modifiers = modifiers
    @proficiencies = proficiencies
    @skills = skills
    @languages = languages
    #inventory
    @inventory = inventory
    @equipment = equipment
    @coins = coins
    #features
    @features = features
    @feats = feats
    #casting
    @spellsKnown = spellsKnown
    @spellSlots = spellSlots
    @maxSpellSlots = maxSpellSlots
    @currentSpellSlots = currentSpellSlots
    @cantrips = cantrips
  end
end
ruby hash
2个回答
0
投票

[我认为您应该简化自己的工作,然后执行类似的操作:


-1
投票

© www.soinside.com 2019 - 2024. All rights reserved.