根据其他实例变量值将Ruby实例变量创建为默认值

根据其他实例变量值将Ruby实例变量创建为默认值,ruby,instance-variables,Ruby,Instance Variables,我正在创建一个以散列作为参数的Ruby类: class Player include PlayerHelper attr_accessor :at_bats, :hits, :walks, :hbp, :sac_flies, :singles, :doubles, :triples, :hr, :put_outs, :assists, :errors, :er, :ip, :so, :stolen_bases, :caught_steali

我正在创建一个以散列作为参数的Ruby类:

class Player
include PlayerHelper
attr_accessor :at_bats, :hits, :walks, :hbp, :sac_flies, :singles, :doubles,
              :triples, :hr, :put_outs, :assists, :errors, :er, :ip, :so,
              :stolen_bases, :caught_stealing
def initialize(hash)
  @at_bats = hash.fetch(:at_bats, nil)
  @hits = hash.fetch(:hits, nil)
  @walks = hash.fetch(:walks, nil)
  @hbp = hash.fetch(:hbp, nil)
  @sac_flies = hash.fetch(:sac_flies, nil)
  @singles = hash.fetch(:singles, nil)
  @doubles = hash.fetch(:doubles, nil)
  @triples = hash.fetch(:triples, nil)
  @hr = hash.fetch(:hr, nil)
  @put_outs = hash.fetch(:put_outs, nil)
  @assists = hash.fetch(:assists, nil)
  @errors = hash.fetch(:errors, nil)
  @er = hash.fetch(:er, nil)
  @ip = hash.fetch(:ip, nil)
  @walks = hash.fetch(:walks, nil)
  @hits = hash.fetch(:hits, nil)
  @so = hash.fetch(:so, nil)
  @stolen_bases = hash.fetch(:stolen_bases, nil)
  @caught_stealing = hash.fetch(:caught_stealing, nil)
end
我想让用户选择包含:
singles
,然后首先检查hash中是否包含
:singles
。如果是这样,请给它哈希值。这部分我一直在工作

我无法工作的是,如果:
singles
键不存在,则为@code>singles提供
:hits-(:doubles+:triples+:hr)的值。我已经尝试过一开始单独调用一个方法,但这似乎不起作用


如果没有
:singles
键,如何根据其他散列值设置
@singles
的值?

使用
|124; 124;=
这是
neu=neu | neu | old
的语法糖,设置新值,前提是之前没有设置(等于
nil


使用
| |=
,这是
neu=neu | | old
的语法糖,设置新值(如果且仅当之前未设置该值时)(等于
nil


这就是
fetch
方法的第二个参数的作用:

def initialize(hash)
  # ...
  @hits = hash.fetch(:hits, nil)
  @doubles = hash.fetch(:doubles, nil)
  @triples = hash.fetch(:triples, nil)
  @hr = hash.fetch(:hr, nil)
  @singles = hash.fetch(:singles, @hits - (@doubles + @tripples + @hr))
  # ...
end

但是,请注意,由于您将所有值默认为
nil
,如果这些值未传递到构造函数中,则可能会在nil:NilClass
-类型上出现
未定义的方法错误!您可能希望设置一些不同的默认值,或使它们成为必需的参数…

这就是
fetch
方法的第二个参数可以用于:

def initialize(hash)
  # ...
  @hits = hash.fetch(:hits, nil)
  @doubles = hash.fetch(:doubles, nil)
  @triples = hash.fetch(:triples, nil)
  @hr = hash.fetch(:hr, nil)
  @singles = hash.fetch(:singles, @hits - (@doubles + @tripples + @hr))
  # ...
end

但是,请注意,由于您将所有值默认为
nil
,如果这些值未传递到构造函数中,则可能会在nil:NilClass
-类型上出现
未定义的方法错误!您可能希望设置一些不同的默认值,或者将它们设置为必需的参数…

“这似乎不起作用”不是一个足够精确的错误描述,我们无法帮助您。什么不起作用?它怎么不起作用?你的代码有什么问题?你收到错误信息了吗?错误消息是什么?你得到的结果不是你期望的结果吗?你期望得到什么样的结果?为什么?你会得到什么样的结果?两者有什么不同?你观察到的行为是否不是期望的行为?期望的行为是什么?为什么?观察到的行为是什么?它们有什么不同?还有,你所说的“这似乎不起作用”是什么意思?它行得通还是不行?请同时确保您确实提供了一个。在你的情况下,我高度怀疑你的例子是最小的,我很确定它不需要26行来证明你的问题。另外,您的示例不完整,它缺少
PlayerHelper
的定义。迭代散列不是更容易吗
hash.each{k,v|public_send(“{k}=”,v)}
“这似乎不起作用”不是一个足够精确的错误描述,我们无法帮助您。什么不起作用?它怎么不起作用?你的代码有什么问题?你收到错误信息了吗?错误消息是什么?你得到的结果不是你期望的结果吗?你期望得到什么样的结果?为什么?你会得到什么样的结果?两者有什么不同?你观察到的行为是否不是期望的行为?期望的行为是什么?为什么?观察到的行为是什么?它们有什么不同?还有,你所说的“这似乎不起作用”是什么意思?它行得通还是不行?请同时确保您确实提供了一个。在你的情况下,我高度怀疑你的例子是最小的,我很确定它不需要26行来证明你的问题。另外,您的示例不完整,它缺少
PlayerHelper
的定义。迭代散列不是更容易吗
hash.each{k,v | public_send(“{k}=”,v)}
namererror
,在
初始化中没有名为
all
的局部变量。谢谢!感谢您的帮助
namererror
,在
initialize
中没有名为
all
的局部变量。谢谢!谢谢你的帮助