使用"?"初始化实例变量时

时间:2015-07-18 15:55:50

标签: ruby instance-variables

我想让sushiable?返回initialise中设置的实例变量,但我似乎无法让它工作。我做错了什么?

attr_accessor :weight, :value, :sushiable?

def initialize (weight,value, sushiable?)
  @weight = weight
  @value = value
  @sushiable = sushiable?
end

# def sushiable?
#   false
# end

1 个答案:

答案 0 :(得分:4)

使用?仅对方法名称有效,对变量无效。所以,正确的方法是:

attr_accessor :weight, :value, :sushiable

def initialize (weight, value, sushiable)
  @weight = weight
  @value = value
  @sushiable = sushiable
end

def sushiable?
  sushiable
end