了解 attr_accessor 类方法的 self
class Test
class << self
attr_accessor :some
def set_some
puts self.inspect
some = 'some_data'
end
def get_some
puts self.inspect
some
end
end
end
Test.set_some => Test
puts Test.get_some.inspect => Test nil
上面我可以找到 self 作为 Test 本身,但不会返回 some_data
作为输出。
但是,当我按以下方式修改时,它返回预期输出
class Test
class << self
attr_accessor :some
def set_some
puts self.inspect
self.some = 'some_data'
end
def get_some
puts self.inspect
self.some
end
end
end
Test.set_some => Test
puts Test.get_some.inspect => Test some_data
有什么区别?
编辑
现在,在第一个示例中,如果我将 get some
方法设置为
Test.some = 'new_data'
puts Test.some.inspect #=> new_data
Test.set_some
puts Test.get_some.inspect => new_data
Now,它会让我更加困惑。
class Test
class << self
attr_accessor :some
def set_some
puts self.inspect
some = 'some_data'
end
def get_some
puts self.inspect
some
end
end
end
Test.set_some => Test
puts Test.get_some.inspect => Test nil
Here above I could find self as Test itself but not returning the some_data
as output.
But while I modified in following way it returns expected output
class Test
class << self
attr_accessor :some
def set_some
puts self.inspect
self.some = 'some_data'
end
def get_some
puts self.inspect
self.some
end
end
end
Test.set_some => Test
puts Test.get_some.inspect => Test some_data
What is the differences?
EDIT
Now in the first example if I set as get some
method as
Test.some = 'new_data'
puts Test.some.inspect #=> new_data
Test.set_some
puts Test.get_some.inspect => new_data
Now it made me much more confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
some = :foo
使 ruby 认为它应该创建一个名为some
的新局部变量。如果您想调用some=()
,则必须使用显式接收器 - 如self.some = :foo
中所示。我曾经打赌输过......:-/some = :foo
makes ruby think it should create a new local variable with namesome
. If you want to callsome=()
, you have to use an explicit reciever - as inself.some = :foo
. I once lost a bet on that... :-/在第一个示例中它是(局部)变量
It's (local) variable in the first example
在第一个示例中,
some
是一个局部变量。在第二个中,
some
是self
的一种方法。为什么?因为attr_accessor :some
与:所以,您已经为实例变量
@some
创建了 getter 和 setter 方法(它是对象的实例变量)测试
,因为每个类也是类Class
的对象)。In the first example
some
is a local variable.In the second one,
some
is a method ofself
. Why? Becauseattr_accessor :some
is the same as:So, you have created the getter and setter methods for the instance variable
@some
(it's an instance variable of the objectTest
, as every class is also an object of classClass
).在第一个方法中,
some 是局部变量。它与 @some 不同,@some 是实例变量(在本例中是类实例变量),因此该值在方法结束时消失。
如果您想调用 setter 方法 some 或将 @some 设置为某些内容,请执行此操作
或
在第二个方法中
调用该方法 some 。它返回实例变量@some..并且因为此时@some没有值..返回nil..
in the first method
some is a local variable.. its not the same as @some that is a instance variable (in this case a class instance variable) so the value disappears when the method ends.
if you want to call the setter method some or set @some to something then do this
or
in the second method
your calling the method some. which returns the instace variable @some.. and since at this point @some has no value.. returns nil..
示例 1 没有方法重写,也没有局部变量
@foo、self.foo 和 foo 将在实例方法中访问实例变量 @foo:
示例 2 使用方法重写
@foo 将访问实例变量,但 self.foo 和 foo 将调用 foo 重写方法:
示例 3 具有方法重写和局部变量
@foo 访问实例变量,self.foo 访问重写方法,foo 访问局部变量:
Example 1 with no method override and no local variable
@foo, self.foo, and foo will access instance variable @foo within the instance method:
Example 2 with method override
@foo will access the instance variable, but self.foo and foo will call the foo override method:
Example 3 with method override and local variable
@foo accesses instance variable, self.foo accesses override method, and foo accesses local variable: