试图理解在 Ruby 中使用 self.method_name 与 Classname.method_name [英] Trying to understand use of self.method_name vs. Classname.method_name in Ruby

查看:33
本文介绍了试图理解在 Ruby 中使用 self.method_name 与 Classname.method_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解何时使用 self.method_name 与何时使用 Classname.method_name.

I'm trying to understand when to use self.method_name vs. when to use Classname.method_name.

在下面的示例中,为什么before_create"需要引用User.hash_password"而不是self.hash_password"或仅引用hash_password"?

In the example below, why does "before_create" need to reference "User.hash_password" instead of "self.hash_password" or just "hash_password"?

因为我们已经在 User 类中,我认为 before_create 方法会知道"hash_password"是它自己类的成员,不需要任何特殊的语法来引用它.

Since we are in the User class already, I thought the before_create method would "know" that "hash_password" is a member of its own class and would not need any special syntax to refer to it.

require 'digest/sha1'

class User < ActiveRecord::Base

  attr_accessor :password
  attr_accessible :name, :password

  validates_presence_of :name, :password
  validates_uniqueness_of :name

  def before_create
    self.hashed_password = User.hash_password(self.password)
  end

  def after_create
    @password = nil
  end

  def self.login(name, password)
    hashed_password = hash_password(password || "")
    self.find(:first, :conditions => ["name = ? and hashed_password = ?", name, hashed_password])
  end

  def try_to_login
    User.login(self.name, self.password)
  end

  private

  def self.hash_password(password)
    Digest::SHA1.hexdigest(password)
  end

end

推荐答案

def before_create
   self.hashed_password = User.hash_password(self.password)
end

在这个例子中,User.hash_password 调用了 class User 上的 hash_password 方法,而 >self.hashed_pa​​ssword=User这个特定实例上调用 hashed_pa​​ssword= 方法.

In this example, User.hash_password calls the hash_password method on the class User, whereas self.hashed_password= calls the hashed_password= method on this particular instance of User.

如果你用 self.hash_password 替换 User.hash_password,Ruby 会抱怨 NoMethodError,因为没有实例方法的名称hash_password 存在于 User 类中.不过,您可以将其替换为 self.class.hash_password.

If you replace User.hash_password with self.hash_password, Ruby would complain with a NoMethodError, because no instance method by the name of hash_password exists in the class User. You could replace it with self.class.hash_password, though.

如果将 self.hashed_pa​​ssword= 替换为简单的 hashed_pa​​ssword=,Ruby 将创建一个名为 hashed_pa​​ssword 的局部变量,而不是调用实例方法 hashed_pa​​ssword=.如果要调用属性编写器,则需要显式添加 self.

If you replace self.hashed_password= with simply hashed_password=, Ruby would create a local variable named hashed_password, rather than call the instance method hashed_password=. You need to explicitly add self if you want to call attribute writers.

方法定义中的self (def self.hash_password) 使hash_password 成为类方法而不是实例方法.在这种情况下,self 指的是 class.在实例方法的上下文中,self 指的是一个实例.

The self in the method definition (def self.hash_password) makes hash_password a class method instead of an instance method. In this context, self refers to the class. In the context of an instance method, self refers to an instance.

这篇关于试图理解在 Ruby 中使用 self.method_name 与 Classname.method_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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