- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
所以我想到了这个,想知道当下面的一些事情完成后会发生什么。
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
的实例方法。当时只有我能看到John
和John=
这两个方法。这是怎么发生的?类方法的运行如何动态地向已创建的对象添加方法?。谁能给我解释一下。
代码的输出是:
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/
我正在我的程序中设置一些跟踪代码,想知道哪些方法是通过attr_accessor 定义的。使用 TracePoint,我可以检测何时调用 attr_accessor,但我不知道如何让它告诉我它收到的参
我正在使用 data_mapper/sinatra 并尝试使用 attr_accessor 创建一些属性。以下示例代码: require 'json' class Person include D
我有父类,还有一些子类 class Base end class ChildNameAge < Base ATTRIBUTES = [:name, :age] attr_accessor *A
我想限制子类中父类(super class)方法的访问 class Parent attr_accessor :first_name, :last_name def initialize(fi
我想知道如何即时创建 attr_accessor。是否可以做类似的事情: attr_accessor "@#{key}" 在初始化方法中? module Mymodule def initiali
我不明白为什么我们需要在类中声明 attr_reader 和 attr_writer,或 attr_accessor。我都读了 this和 this帖子,但这些帖子主要解释它们是如何工作的,而不是它们
我有一个 attr_accessor 设置如下的类: class Human ATTRIBUTES = [:name] attr_accessor *ATTRIBUTES end 它就
我正在尝试创建一个包含 Singleton 的类使用 attr_accessor 时的模块。但这似乎不起作用。 require 'singleton' class Foo attr_accesso
我知道 Ruby 支持调用方法的简写风格,即:1.+(2) 与 1+2 相同(我仍然认为如果它与我的情况有关),但我真的很困惑为什么 attr_accessor 方法既不重复(好吧,它们不应该重复,因
http://gist.github.com/172341 (stackoverflow 破坏了格式) 在以下情况下,Human 创建的方法名称对 Boy 不可用。我的理解是否正确,attr_acce
我想使用 attr_accessor 将数组作为实例变量。 但是 attr_accessor 不只是用于字符串吗? 如何在阵列上使用它? 更新: 例如。如果你想: object.array = "ca
我正在尝试在 initialize 中执行一个 instance_eval,然后执行一个 attr_accessor,并且我一直得到这个:``initialize':未定义的方法“attr_acces
我在我的类中动态创建了一个实例变量: class Mine attr_accessor :some_var def intialize @some_var = true end
attr_accessor 不适用于以下代码。错误显示“父级未定义方法‘事物’(NoMethodError)”: class Parent @@things = [] attr_accesso
我理解 x |= y 基本上意味着 x = x|y。运算符 | 为类 Set 定义为计算两个集合的并集。确实我们可以看到: require 'set' r = Set.new(%w(a b)); s
我不明白为什么要这样写: class Building < ActiveRecord::Base attr_accessor :has_child_custom, 'test' end 然后我
我有一个简单的类,并且正在转置一个二维数组,如下所示: class Group attr_accessor :group_array def initialize @group_arr
我有一个简单的类,并且正在转置一个二维数组,如下所示: class Group attr_accessor :group_array def initialize @group_arr
这可能与 this question 完全相反.考虑以下规范: describe Record do it "calculates the first term grade" do tes
我正在使用 Sinatra 和 RSpec。我在 lib/auth.rb 中有这个 class Person attr_accessor :password if ENV['RACK_ENV'
我是一名优秀的程序员,十分优秀!