我很难理解Ruby中的attr_accessor。 有人能给我解释一下吗?
当前回答
如果你熟悉面向对象的概念,你必须熟悉getter和setter方法。 在Ruby中attr_accessor做同样的事情。
Getter和Setter的一般方式
class Person
def name
@name
end
def name=(str)
@name = str
end
end
person = Person.new
person.name = 'Eshaan'
person.name # => "Eshaan"
Setter方法
def name=(val)
@name = val
end
Getter方法
def name
@name
end
在Ruby中的Getter和Setter方法
class Person
attr_accessor :name
end
person = Person.new
person.name = "Eshaan"
person.name # => "Eshaan"
其他回答
Attr_accessor只是一个方法。(这个链接应该提供更多关于它如何工作的见解——看看生成的方法对,一个教程应该告诉你如何使用它。)
关键在于,类在Ruby中不是定义(在c++和Java等语言中“只是定义”),而是求值的表达式。在此求值期间,调用attr_accessor方法,该方法反过来修改当前类——记住隐式接收器:self。其中self是此时“打开”的类对象。
对attr_accessor和friends的需求是:
像Smalltalk一样,Ruby不允许在该对象的method1之外访问实例变量。也就是说,不能像在Java甚至Python中那样以x.y形式访问实例变量。在Ruby中,y总是被当作一个要发送的消息(或“要调用的方法”)。因此attr_*方法创建包装器,通过动态创建的方法代理实例@变量访问。 样板很糟糕
希望这能澄清一些细节。快乐的编码。
1严格来说,这并不正确,有一些“技术”围绕这一点,但没有语法支持“公共实例变量”访问。
attrr -accessor简单地为指定的属性创建getter和setter方法
简单的解释,没有任何代码
以上大多数答案都使用代码。这个解释试图通过一个类比/故事来回答这个问题:
外界不能接触CIA内部机密
Let's imagine a really secret place: the CIA. Nobody knows what's happening in the CIA apart from the people inside the CIA. In other words, external people cannot access any information in the CIA. But because it's no good having an organisation that is completely secret, certain information is made available to the outside world - only things that the CIA wants everyone to know about of course: e.g. the Director of the CIA, how environmentally friendly this department is compared to all other government departments etc. Other information: e.g. who are its covert operatives in Iraq or Afghanistan - these types of things will probably remain a secret for the next 150 years. If you're outside the CIA you can only access the information that it has made available to the public. Or to use CIA parlance you can only access information that is "cleared". The information that the CIA wants to make available to the general public outside the CIA are called: attributes.
读写属性的含义:
In the case of the CIA, most attributes are "read only". This means if you are a party external to the CIA, you can ask: "who is the director of the CIA?" and you will get a straight answer. But what you cannot do with "read only" attributes is to make changes changes in the CIA. e.g. you cannot make a phone call and suddenly decide that you want Kim Kardashian to be the Director, or that you want Paris Hilton to be the Commander in Chief. If the attributes gave you "write" access, then you could make changes if you want to, even if you were outside. Otherwise, the only thing you can do is read. In other words accessors allow you to make inquiries, or to make changes, to organisations that otherwise do not let external people in, depending on whether the accessors are read or write accessors.
类中的对象可以很容易地相互访问
另一方面,如果你已经在中央情报局内部,那么你可以很容易地打电话给你在喀布尔的中央情报局特工,因为这些信息很容易获得,因为你已经在里面了。但如果你不在中情局之外,你根本就没有权限:你不能知道他们是谁(读权限),你也不能改变他们的任务(写权限)。
类也是如此,你可以访问其中的变量、属性和方法。HTH !任何问题,请问,我希望我能澄清。
定义此模块的命名属性,其中名称为symbol。Id2name,创建实例变量(@name)和读取它的相应访问方法。还创建一个名为name=的方法来设置属性。
module Mod
attr_accessor(:one, :two)
end
Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]
它只是一个为实例变量定义getter和setter方法的方法。一个示例实现如下:
def self.attr_accessor(*names)
names.each do |name|
define_method(name) {instance_variable_get("@#{name}")} # This is the getter
define_method("#{name}=") {|arg| instance_variable_set("@#{name}", arg)} # This is the setter
end
end
推荐文章
- 了解Gemfile。锁文件
- 如何确定一个数组是否包含另一个数组的所有元素
- 是什么导致这个ActiveRecord::ReadOnlyRecord错误?
- 最好的打印散列的方法
- Rails:dependent =>:destroy VS:dependent =>:delete_all
- 我可以在不包含Ruby模块的情况下调用实例方法吗?
- 如何将新项目添加到哈希
- 测试一个Ruby类是否是另一个类的子类
- 什么时候使用Struct vs. OpenStruct?
- 数组到哈希Ruby
- 我如何让红宝石打印一个完整的回溯而不是截断一个?
- 如何使用RSpec的should_raise与任何类型的异常?
- 如何创建退出消息
- 忽略GEM,因为没有构建它的扩展
- 在Gem::Specification.reset期间未解决的规格: