Total newbie: Instance variables in ruby?

后端 未结 4 1336
旧巷少年郎
旧巷少年郎 2020-12-01 11:39

Pardon the total newbiew question but why is @game_score always nil?

#bowling.rb

class Bowling
  @game_score = 0
    def hit(pins)
        @game_score = @ga         


        
4条回答
  •  -上瘾入骨i
    2020-12-01 12:02

    @game_score defined there is called class instance variable, which is a variable defined for the singleton class object:

    class << Bowling
      attr_accessor :game_score
    end
    
    Bowling.game_score #=> 0
    

    This is as you can tell different from the normal instance variables defined for instance objects.

提交回复
热议问题