0

可能重复:
如何访问此变量?

可以说我有代码:

class Player
  def getsaves
    print "Saves: "
    saves = gets
  end
  def initialize(saves, era, holds, strikeouts, whip)
  end
end

我希望能够在初始化方法中访问getsaves中的saves变量说:

j = Player.new(getsaves_saves_variable, 30, 30, 30, 30)
4

1 回答 1

0

定义属性:

attr_accessor :saves_attr
attr_accessor :era_attr
attr_accessor :holds_attr
attr_accessor :strikeouts_attr
attr_accessor :whip_attr

def initialize(saves, era, holds, strikeouts, whip)
   self.saves = saves
   self.era_attr = era
   ...
end

然后在getsaves中你可以这样做:

self.saves_attr = gets
于 2012-04-07T19:02:11.900 回答