English 中文(简体)
为什么使用Ruby str_accessor, attr_reader and attr_author?
原标题:Why use Ruby s attr_accessor, attr_reader and attr_writer?

Ruby利用诸如钥匙等钥匙分享典型变数的手法和方便的方法

attr_accessor :var
attr_reader :var
attr_writer :var

如果我能简单地使用<代码>attr_accessor/code>? 是否有这样的业绩(我怀疑)? 我猜想有这样的理由,否则他们就没有这样做。

最佳回答

您可使用不同的查阅者向读过你的密码的人传达你的意向,并方便其撰写班级,不管其公开的读者身份如何,这些班级都将是正确的。

class Person
  attr_accessor :age
  ...
end

在此,我可以看到,我可以读写年龄。

class Person
  attr_reader :age
  ...
end

在此,我可以看到,我只能读到年龄。 试想的是,它是由这一类的构造者确定的,而且之后仍然不变。 如果有一个年龄的变数人(作者),并且该班级被写成,假定年龄一旦确定,不会改变,那么,根据呼吁变换者的法典,可能会产生一种诱变。

但幕后发生的情况如何?

请写:

attr_writer :age

这转化为:

def age=(value)
  @age = value
end

请写:

attr_reader :age

这转化为:

def age
  @age
end

请写:

attr_accessor :age

这转化为:

def age=(value)
  @age = value
end

def age
  @age
end

了解这一点,这里又是想到的另一种方式:如果你没有 at的......助手,并且必须写出入口,那么你会写出比你的班子需要的更多的入口。 例如,如果仅需要阅读年龄,那么你是否也写出一种允许书写年龄的方法?

问题回答

以上所有答复都是正确的;attr_readerattr_ author<>/code>比人工打上其简短手法更为方便。 除此以外,它们提供的业绩远远优于撰写方法定义。 欲了解更多信息,请参看从( PDF)。

It is important to understand that accessors restrict access to variables, but not their content. In ruby, like in some other OO languages, every variable is a pointer to an instance. So if you have an attribute to an Hash, for example, and you set it to be "read only" you always could change its content, but not the content of pointer. Look at this:

> class A
>   attr_reader :a
>   def initialize
>     @a = {a:1, b:2}
>   end
> end
=> :initialize
> a = A.new
=> #<A:0x007ffc5a10fe88 @a={:a=>1, :b=>2}>
> a.a
=> {:a=>1, :b=>2}
> a.a.delete(:b)
=> 2
> a.a
=> {:a=>1}
> a.a = {}
NoMethodError: undefined method `a=  for #<A:0x007ffc5a10fe88 @a={:a=>1}>
        from (irb):34
        from /usr/local/bin/irb:11:in `<main> 

由于你可以看到,从哈希姆瓦(a)中删除一个关键/价值乳制品,作为添加新的钥匙、改变价值、ec。 但是,你可以指出一个新目标,因为只有试样。

并非所有物体的属性都直接从该类别外确定。 你们所有变量的作者一般都表明,你在课堂之间重新出现太多的争.。

例如: 我写了一个设计方案,在集装箱内放置物品。 该项目有<条码>attr_reader :container <<>/code>,但提供作者并无意义,因为该件集装箱唯一应当变换的时间是放在新集装箱内,这也需要定位信息。

你们并不总是希望能够从该类外充分获取你的假设变量。 很多情况下,允许读取试样变量是有意义的,但书写可能并不(例如,从只读来源检索数据的模式)。 在有些情况下,你们想要的是相反的,但我想到的是,有人在我头顶上争.。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...