gpt4 book ai didi

ruby-on-rails - @@ 在 Ruby 中是什么意思?

转载 作者:数据小太阳 更新时间:2023-10-29 07:17:08 28 4
gpt4 key购买 nike

当我浏览 Rails 源代码时,它包含以下行:

@@autoloads = {}

@@ 在 Ruby 中是什么意思?

最佳答案

这意味着访问类属性(命名空间为类的属性),而不是实例属性(为该类的每个实例化对象而存在的属性)。

在您的示例中,@@autoloads 将在您的程序长度内持续存在。

class TestObj
@@prop = 0
def get_prop
@@prop
end

def increment_prop
@@prop += 1
end
end

a = TestObj.new
b = TestObj.new

a.increment_prop

puts b.get_prop # 1

CodePad

关于ruby-on-rails - @@ 在 Ruby 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11131883/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com