Hi --

On Thu, 11 Jan 2007, Paul Smith wrote:

> Newbie question:
>
> In the following code:
>
>  class Person < ActiveRecord::Base
>     validates_presence_of :first_name
>  end
>
> validates_presence_of is a method call that is
> specified outside of any method in class Person.
>
> Could someone explain what the invocation model is
> for that statement that is not bound to any method
> within a class (i.e. when is that statement executed?)

It's the same, in the abstract, as the invocation model generally: a
"bareword"-style method is automatically invoked on the current
default object, self.  At the outer level of a class definition, self
is the class object itself.  So what you're seeing is a call to a
class method.

Here's a little X-ray of what's going on:

   class SomeClass
     def self.do_something
       puts "Hi!"
     end
   end

   class C < SomeClass
     puts "self right now is #{self}."   # self right now is C.
     do_something                        # Hi!
   end

There are more nuances but that's the basic scenario.


David

-- 
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)