使用 Ruby 调用虚拟属性中的实例变量

发布于 2024-10-15 15:31:16 字数 701 浏览 6 评论 0原文

所以我刚刚开始阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔少女心 2024-10-22 15:31:16

这是无接收者消息发送。

在 Ruby 中,接收者 self 是隐式的:如果您愿意,可以将其省略。因此,priceself.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 as self.price (ignoring access restrictions).

In other words, it's calling the method price you defined with attr_accessor.

丘比特射中我 2024-10-22 15:31:16

您可以轻松确认两个引用(price@price)指向相同的对象:

#!/usr/bin/env ruby

class MyPrice < Object
  attr_accessor :price

  def initialize(price = 0)
    @price = price
  end

  def price_in_cents
    Integer(price * 100 + 0.5)
  end

  def objects
    puts "@price.object_id: #{@price.object_id}"
    puts "price.object_id:  #{price.object_id}"
  end
end

p = MyPrice.new(150)
p.objects
puts "price in cents: #{p.price_in_cents}"

这会产生:

# ./price.rb 
@price.object_id: 301
price.object_id:  301
price in cents: 15000

price 和 < code>@price 的对象 ID 均为 301,这表明它们都是对单个对象的引用。

You can easily confirm that that two references (price and @price) point to the same objects:

#!/usr/bin/env ruby

class MyPrice < Object
  attr_accessor :price

  def initialize(price = 0)
    @price = price
  end

  def price_in_cents
    Integer(price * 100 + 0.5)
  end

  def objects
    puts "@price.object_id: #{@price.object_id}"
    puts "price.object_id:  #{price.object_id}"
  end
end

p = MyPrice.new(150)
p.objects
puts "price in cents: #{p.price_in_cents}"

Which produces:

# ./price.rb 
@price.object_id: 301
price.object_id:  301
price in cents: 15000

The fact that price and @price both have an object id of 301 shows you that they are both references to a single object.

祁梦 2024-10-22 15:31:16

这个问题是几年前发布的,但以防万一有人遇到类似的问题,比如上面的例子。

我不认为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文