【发布时间】:2011-08-18 21:46:50
【问题描述】:
什么是前面带有双 at 符号 (@@) 的 Ruby 变量?我对以 at 符号开头的变量的理解是它是一个实例变量,就像 PHP 中这样:
PHP版本
class Person {
public $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
Ruby 等效
class Person
def set_name(name)
@name = name
end
def get_name()
@name
end
end
@@ 的双 at 符号是什么意思,它与单 at 符号有何不同?
【问题讨论】:
-
我不知道,但我感觉它在盯着我看。我现在有点害怕用 Ruby 编写代码......
-
TL;公众博士:100 次中有 99 次,我会使用“类实例”变量(
self方法中的@)而不是类变量(@@)。请参阅下面的答案中的一连串原因。
标签: ruby syntax instance-variables class-variables