0

There is the following code in RoR:

class Product < ActiveRecord::Base
  validates :title, :description, :image_url, presence: true
end

I've read some Ruby books, but I didn't see in OOP paragraph any information about executing methods of class in class (not other method) body! What is it? How does it work? When is this method executed? Please, make me clear in this question. Thanks.

2
  • you need to go codeacadamy.. They explained these.. Commented Jul 20, 2013 at 6:44
  • Metaprogramming Ruby book - it has all answers! Commented Jul 20, 2013 at 6:54

1 Answer 1

2

A method is executed when it is called. In the context of a class body, self becomes that class. self as a receiver can be omitted, so validates here is the same as Product.validates ... or self.validates .... validates is a class method on Active::Base, and is called during the class definition.


In my understanding, self and an implicit receiver mean different things depending on where it is located.

module Foo
  # module body
  def # method name
    # method body
  end
end
  1. In a module body
    • self: the module
    • implicit receiver: the module
  2. In a method name
    • self: the module
    • implicit receiver: an instance of the class
  3. In a method body
    • self: the receiver in the method name
    • implicit receiver: the receiver in the method name
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.