gpt4 book ai didi

ruby - 当 attr_accessor 在类方法中时会发生什么?

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

所以我想到了这个,想知道当下面的一些事情完成后会发生什么。

 class Test 
def self.abc
attr_accessor :John
end
end

object = Test.new
puts "before calling class method abc: #{object.class.instance_methods(false)}"
Test.abc
puts "after calling class method abc: #{object.class.instance_methods(false)}"

这里我检查的是,getter 和 setter 方法是否以这种方式创建。如果是这样,是那些实例方法或类方法。首先我创建一个新对象,然后查看该对象的实例方法是什么。在下一行之后,我运行 class 方法 abc 然后再次检查 object 的实例方法。当时只有我能看到JohnJohn=这两个方法。这是怎么发生的?类方法的运行如何动态地向已创建的对象添加方法?。谁能给我解释一下。

代码的输出是:

before calling class method abc:  []
after calling class method abc: [:John, :John=]

最佳答案

在类方法中,self 是类本身。因此,以下内容在它们最终所做的事情上是等价的:

class Test
def self.abc
# `self` in this context is Test.
# This sends the message `:attr_accessor, :john` to `Test`
attr_accessor :john
end
end

class Test
# `self` in this context is Test.
# This sends the message `:attr_accessor, :john` to `Test`
attr_accessor :john
end

但是,正如您所指出的,Test::abc 不会在类被解析时执行,因此 attr_accessor 不会被调用,实例方法也不会被调用t补充道。在运行时执行此操作是完全有效的,事实上,这是在 Rails 中执行的大部分元编程的基础。

通常,如果您希望通过类方法添加访问器,您可以在定义之后但仍在类声明期间调用该类方法:

class Test
def self.abc
attr_accessor :john
end

abc
end

这将实际运行,并在类上正确声明访问器!

关于你的问题:

How come a running of a class method dynamically add methods to already created objects?

这是因为实例化一个类并不会在实例化时实例化该类的“快照”——它创建了一个对象,该对象将其大部分功能(包括发现其上的实例方法)委托(delegate)给关联的类用它。请注意,也可以在实例上定义新方法,而这些方法也不会扩展回类:

class Test
attr_accessor :foo
end

t1 = Test.new
t2 = Test.new

Test.send(:define_method, :bar) {}
puts t1.respond_to? :foo # => true
puts t2.respond_to? :foo # => true

puts t1.respond_to? :bar # => true
puts t2.respond_to? :bar # => true

t1.define_singleton_method(:baz) {}

puts t1.respond_to? :baz # => true
puts t2.respond_to? :baz # => false

关于ruby - 当 attr_accessor 在类方法中时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41986965/

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