Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
988 views
in Technique[技术] by (71.8m points)

ruby on rails - Method invocation in class definition?

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

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" 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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.

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

Consider the following:

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

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

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

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

Here there be logic
Here there be logic

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...