使用 Ruby 调用虚拟属性中的实例变量
所以我刚刚开始阅读 The Pragmatic Bookself 的《Programming Ruby 1.9 (第三版)”,我遇到了一些需要澄清的代码。
如果您拥有这本书,它位于第 3 章“类、对象和变量”,特别是有关虚拟属性的部分。
基本上,一个类是用一个初始化程序来定义的,该初始化程序设置几个实例变量,其中一个是@price
。该变量有一个使用 attr_accessor 创建的访问器/变异器,如下所示:
attr_accessor :price
该类还有一个名为 price_in_cents
的虚拟属性,它只是返回此行中的值:
Integer(price*100 + 0.5)
现在我的问题是为什么 price<虚拟属性中的 /code> 没有以
@
为前缀?它显然是在处理一个实例变量。执行不带 @
的代码与使用 @
执行代码的效果相同;这是为什么?
PS 抱歉,我不只是批量发布代码 - 鉴于这是一个关于书中代码的问题,我不确定我必须发布什么合法权利。
So I am just starting to make my way through The Pragmatic Bookself's, "Programming Ruby 1.9 (3rd Edition)" and I've come across some code that I need a little clarification on.
If you own the book, it's in Chapter 3's, "Classes, Objects, and Variables," specifically in the section about virtual attributes.
Basically, a class is defined with an initializer that sets a couple of instance variables, one of which is @price
. That variable has an accessor / mutator created with attr_accessor like so:
attr_accessor :price
That class also has a virtual attribute called, price_in_cents
which simply returns the value from this line:
Integer(price*100 + 0.5)
Now my question is why is price
in the virtual attribute not prefixed with an @
? It is clearly dealing with an instance variable. Executing the code without the @
works just the same as with; why is that?
P.S. Sorry for not just posting the code wholesale—given that this is a question about code in a book, I wasn't sure what legal right I'd have to post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是无接收者消息发送。
在 Ruby 中,接收者
self
是隐式的:如果您愿意,可以将其省略。因此,price
与self.price
基本相同(忽略访问限制)。换句话说,它调用您使用
attr_accessor
定义的方法price
。That's a receiverless message send.
In Ruby, the receiver
self
is implicit: you can leave it out if you want to. So,price
is basically the same asself.price
(ignoring access restrictions).In other words, it's calling the method
price
you defined withattr_accessor
.您可以轻松确认两个引用(
price
和@price
)指向相同的对象:这会产生:
price
和 < code>@price 的对象 ID 均为 301,这表明它们都是对单个对象的引用。You can easily confirm that that two references (
price
and@price
) point to the same objects:Which produces:
The fact that
price
and@price
both have an object id of 301 shows you that they are both references to a single object.这个问题是几年前发布的,但以防万一有人遇到类似的问题,比如上面的例子。
我不认为 self.price 与 attr_accessor 有任何关系, self.price 仅适用于 attr_accessor 成为实例变量的快捷方式的类。您不需要在 attr_accessor 前面输入“@”,因为如果您熟悉 Java,它可以充当 setter 和 getter 的快捷方式。这是 Ruby 轻松、更快、更有说服力地完成工作的方式的一部分。
This question was posted years back but just in case anybody is running into similar issues like the example on top.
I don't think self.price has anything to do with attr_accessor, self.price only applies to the class where attr_accessor becomes a shortcut for instance variable. You don't need to input the '@' in front of the attr_accessor because it acts as a shortcut for your setters and getters if you are familiar to Java. Part of Ruby's easy going way of getting things done faster more eloquently.