类定义中的方法调用? [英] Method invocation in class definition?

查看:34
本文介绍了类定义中的方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Person < ActiveRecord::Base
  validates :terms_of_service, :acceptance => true
end

在上面,从 Ruby 的角度来看,validates 是什么?它不是方法定义,也不是数据定义或声明.所以,显然它是一个方法调用,就在类的主体中.我从未见过直接在类中调用这样的方法(即在方法定义之外),即使在我现在正在阅读的 Ruby 编程教程中也没有:http://ruby-doc.org/docs/ProgrammingRuby/.

In the above, what is validates from a Ruby standpoint? It's not a method definition, it's not a data definition or declaration. So, evidently it's a method invocation, right there in the body of the class. I have never seen a method invoked like that directly in a class (i.e. outside of a method definition), not even in the Ruby programming tutorial I'm going through now: http://ruby-doc.org/docs/ProgrammingRuby/.

那么,如果它是一个方法调用,它是在什么时候被调用的?我尝试了以下测试:

So, if it's a method invocation, at what point is it being invoked? I tried the following as a test:

class Person   

  print "cat"
    
end

#p = Person.new
#q = Person.new

print "cat" 只执行一次,不管是否声明了任何实际的 Person 对象,所以很明显,在解析类定义时,Ruby 看到方法 print 并说,好的,我现在就继续执行这个";但再也不会这样做了.

print "cat" is being executed exactly once, regardless if any actual Person objects are being declared or not, so evidently just when parsing the class definition, Ruby sees the method print and says, "OK I will just go ahead and execute this now" but never does so again.

那么,可以帮助我了解上述 validates 发生了什么的 Ruby 文档在哪里?

So, where is the Ruby documentation that will help me understand what is going on with validates above?

推荐答案

在 Ruby 中,类声明只是按顺序执行的代码块.

In Ruby, class declarations are just chunks of code, executed in order.

重要的是要记住,在类定义中,self 指向类本身.validatesActiveRecord 的一个类方法.在定义类时,会执行定义中的代码.validates 方法解析为ActiveRecord 的类方法,因此在类定义期间被调用.

It's important to remember that inside a class definition, self points to the class itself. validates is a class method of ActiveRecord. As the class is being defined, code in the definition is executed. The validates method resolves to a class method of ActiveRecord, so is called during class definition.

在您的 Person 示例中,它只会打印一次,因为您只定义了一次类.

In your Person example, it will only print once, because you only define the class once.

考虑以下事项:

class Foo
  def self.validates_nothing(sym)
    (@@syms ||= []) << sym
    puts "!!! Here there be logic"
  end

  def validate
    @@syms.each { |s| puts s }
  end
end

这定义了一个具有类方法 validates_nothing 和实例方法 validate 的类.validates_nothing 只是收集给它的任何参数,validate 只是将它们转储出来.

This defines a class with a class method validates_nothing, and an instance method, validate. validates_nothing just gathers whatever arguments are given it, validate just dumps them out.

class Bar < Foo
  validates_nothing :anything
  validates_nothing :at_all
end

这定义了一个子类.请注意,当调用类方法 validates_nothing 时,它会打印:

This defines a subclass. Note that when the class method validates_nothing is called, it prints:

Here there be logic
Here there be logic

如果我们创建一个新的 bar 并调用 validate,我们会得到预期的输出:

If we create a new bar and call validate, we get the expected output:

> Bar.new.validate
!!!anything
!!!at_all

这篇关于类定义中的方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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