Static variables in ruby, like in C functions

前端 未结 6 1644
花落未央
花落未央 2021-01-22 08:29

Is there any such thing as static variables in Ruby that would behave like they do in C functions?

Here\'s a quick example of what I mean. It prints \"6\\n7\\n\" to the

6条回答
  •  臣服心动
    2021-01-22 08:49

    You can use a global variable:

    $a = 5
    def test
      $a += 1
    end
    
    p test #=> 6
    p test #=> 7
    

提交回复
热议问题