English 中文(简体)
如何使用铁路中的“自己”关键词
原标题:How to use the "self" keyword in Rails

关于铁路的关键词:<代码>自行,我知道,关键词指的是舱体本身。 例如,在<代码>上,加密——密码。

我不太想到的是,作为右侧的参数而通过的“密码><>/代码>号”与“<代码>本身对应的编号。 关键词也如何?

class User < ActiveRecord::Base

  attr_accessor :password
  attr_accessible  :name, :email, :password, :password_confirmation

  validates :password, :presence     => true,
                      :confirmation => true,
                      :length       => { :within => 6..40 }

  before_save :encrypt_password

  private

  def encrypt_password
    self.encrypted_password = encrypt(password)
  end

  def encrypt(string)
    string # Only a temporary implementation!
  end
end

是否有人解释何时使用或不使用<代码>自封/代码>关键词?

问题回答

Answer

答案很简单:范围可见。

def encrypt_password
  self.encrypted_password = encrypt(password)
end

现有(或者,应当在操作时间)tthing,称作password。 就你而言,它从数据库中占有一席之地。 但这也可以是一个地方变量。 如果发现这种名字,就会发现错误。

但是,请在<条码>上注明<条码>加密>的密码<>/条码>与<条码>本身,以明确表明您将重述案情。 否则,将创建新的当地变量<代码>加密-密码。 显然,不是你想要的。

More explanation

这里略微是法典的缩略语。

class Foo
  attr_accessor :var

  def bar1
    var = 4
    puts var
    puts self.var
  end
end

f = Foo.new
f.var = 3
f.bar1

产出

4
3

因此,正如我们能够看到的那样,<代码>var没有<代码>自行。 关键词,由于这一原因,现在在范围上有两个名字:地方变量和案例属性。 当地变量掩盖了原状,因此,如果你真想查阅,则使用<条码>自行/代码>。

The question has nothing to do with Rails, but with Ruby. When you look at your code:

def encrypt_password
  self.encrypted_password = encrypt(password)
end

法文版将翻译为:

  • self.encrypted_password = is a method call to encrypted_password= method.
  • This method is generated by Rails on the fly, because there exists a database column with the same name.
  • <编码>加密(密码)包含2条方法电话。

    1. password calls the method password which is a real attribute of the model by the declaration attr_accessor :password. This declaration creates two methods:
      • password: getter of the attribute
      • password=: setter of the attribute

    见“Sergio_Tulentsev”的解释,“Sergio_Tulentsev”如何能够被当地变数隐藏(在你执行时,没有当地变数)。

    1. Calls the method encrypt with the return value of method call password.

因此,使用<代码>本身。 这表明你(所有时间)采用某种方法,但你不能直接获得归属。 如果你想这样做,你必须使用<条码>@password = <some Value>在一种实例方法中,但我喜欢带<条码>的字体,即“密码=和带”;有些价值和“。

我希望,现在对你的法典有明确的解释。

setter methods => use self

Use self when you need to write something.
In other words, when you need to assign a value.

getter method => don t use self

And without self when you need to read something.
When you need to read a value.

例如:

def write_name(name)
  self.name = name
end

def read_name
  name
end

在您的情形下,虚拟属地password 页: 1 关键词是改为(getter)。

<编码>加密_password系指数据库中储存的内容,即 written<>,载于(setter)中。 因此,<代码>自封/代码>的使用。

Edit:

Michael HARTL对同一案件作了同样的解释:

(Of course, as we’ve noted, the self is not optional when assigning to an attribute, so we have to write self.encrypted_password in this case.)

http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-two#sec:an_active_record_callback

(最后一行7.1.3)





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...