Rails中的默认getter和setter会是什么样子? [英] What would a default getter and setter look like in rails?

查看:120
本文介绍了Rails中的默认getter和setter会是什么样子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以编写attr_accessor:tag_list来为Rails中的对象创建虚拟属性tag_list.这样就可以在对象的表单中包含一个tag_list属性.

I know that I can write attr_accessor :tag_list to make a virtual attribute tag_list for an object in Rails. This allows there to be a tag_list attribute in forms for the object.

如果我使用attr_accessor:tag_list,则可以在模型中对tag_list进行操作,以从表单中提取和处理数据.

If I use attr_accessor :tag_list I can, in the model, perform actions on tag_list to pull and manipulate data from the form.

我想知道的是,我将如何编写一个将完全复制attr_accessor默认功能的getter和setter而不是编写attr_accessor. EG:

What I want to know is, instead of writing attr_accessor, how would I write a getter and setter that would replicate completely the default functionality of attr_accessor. EG:

def tag_list
    #what goes here
end

仅供参考

 def tag_list
     @tag_list
 end

这不起作用.

推荐答案

attr_accessor 是一个内置的Ruby方法,在上下文ActiveRecord中没有特殊含义. attr_accessor :tag_list基本上等同于以下代码:

attr_accessor is a built-in Ruby method and has no special meaning in the context ActiveRecord. attr_accessor :tag_list is basically equivalent to this code:

# getter
def tag_list
  @tag_list
end

# setter
def tag_list=(val)
  @tag_list = val
end

但是,在ActiveRecord模型中,可能是您想要这样的东西:

In ActiveRecord models, however, it could be that you want something like this:

def tag_list
  self[:tag_list]
end

def tag_list=(val)
  self[:tag_list] = val
end

略有不同:使用第一种方法,obj[:tag_list]不会使用与getter和setter相同的存储.有了后者,就可以了.

There is a slight difference: With the first method, obj[:tag_list] doesn't use the same storage as your getter and setter. With the latter, it does.

在Ruby中,以下两行代码是等效的

In Ruby, the following two lines of code are equivalent

thing.blabla
thing.blabla()

都调用对象thing的方法blabla,并求值到该方法中最后求值的表达式.这意味着,在上述getter方法的情况下,您也不需要return语句,因为该方法仅返回方法中的最后一个表达式(@tag_list,实例变量的值).

Both call the method blabla of the object thing and evaluate to the last expression evaluated within that method. This means, you also don't need a return statement in the case of the above getter method, because the method simply returns the last expression in the method (@tag_list, the value of the instance variable).

此外,这两行代码是等效的:

Also, those two lines of code are equivalent:

thing.blabla=("abc")
thing.blabla = "abc"

都调用对象thing的方法blabla=.带有=字符的特殊名称可以与其他任何方法名称一样使用.

Both call the method blabla= of the object thing. The special name with the = character can be used like any other method name.

有时会调用属性实际上是简单的方法,您还可以在返回或接受它们之前使用一些对值进行转换的特殊逻辑.示例:

The fact that attributes, as they are sometimes called, are in fact plain methods, you can also use some special logic transformed on the values before returning or accepting them. Example:

def price_in_dollar
  @price_in_euro * 0.78597815
end

def price_in_dollar=(val)
  @price_in_euro = val / 0.78597815
end

这篇关于Rails中的默认getter和setter会是什么样子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆