rails内のクラスで実行されているこれって一体何だ!?[和訳]

Hey guys :)
今日も和訳するぜ!
stack overflowでrails初心者に興味深い記事があったので紹介します!
まだ私の知識は浅いのできつい和訳になっているので、できれば和訳を参考に英文を読んでいただければと思います汗

–質問–

I have just started learning ruby on rails and I have encountered code like below:

Ruby on Railsを始めたばかりなんだけど、下記のようなコードに出会った。

class Post < ActiveRecord::Base
 validates_presence_of   :title
 belongs_to :user
end

There are two method calls inside the class body. I have had a hard time finding any ruby documentation that describes how method calls from within a body of a class (but outside of any method) work.

クラス内で2つのメソッドが呼び出しをしている。どうやってメソッドが呼び出しをしているのかRubyのドキュメントを必死に探してもみつからない。

All the books I have, only describe how to define class and instance methods and how to call them from within other methods.

私が持っている本には全てどのようにクラスやインスタンスメソッドを作って、どうやって他のメソッドから呼び出せるのかしか説明されていないんだ。

The questions I have are: How and when are these methods called? How are they defined? Are they mixins defined in some active record module?

私の質問は..いつどのようにこれらのメソッドは呼び出されるのか。どのように定義されているのか。ActiveRecord的なものの中で定義されているのか?

–回答–

The body of a class definition is an execution context for code just like any other.

クラスの定義の主要部分としてはコードを実行することができるということである。

Code there is executed within the context of the class (meaning self is the class object, which is an instance of Class).

クラス内のコードが実行されている。(selfはインスタンスクラスであるクラスオブジェクトという意味)

You can have locals and instance variables (which will belong to the class object itself rather than instances of the class) and you can call any method that class object responds to.

インスタンスクラスよりもクラスオブジェクトに付随している)ローカル、インスタンス変数を持つことが出来、クラスオブジェクトが対応するメソッドを呼び出すことが出来る。

The code is run once the class definition block is finished.

コードはクラスで定義されたブロックを通過すると実行される。

In this case, ActiveRecord::Base defines the class methods validates_presence_of and belongs_to.

今回のケースだと、ActiveRecord::Baseがvalidates_presence_ofとbelongs_toのクラスメソッドを定義しているのだ。

ActiveRecord内で定義したものを実行した結果は以下である(例として)。

[11] pry(main)> ActiveRecord::Base.belongs_to(:blog_post)
=> {"blog_post"=>
  #<ActiveRecord::Reflection::BelongsToReflection:0x007fa3ee03c958
   @active_record=ActiveRecord::Base,
   @association_scope_cache={},
   @automatic_inverse_of=nil,
   @constructable=true,
   @foreign_type="blog_post_type",
   @klass=nil,
   @name=:blog_post,
   @options={},
   @plural_name="blog_posts",
   @scope=nil,
   @scope_lock=#<Mutex:0x007fa3ee03c570>,
   @type=nil>}

追記)contextを上手く訳するのが難しかったので省略しましたw

see you!!:)  

リンク

Ruby Method calls declared in class body - Stack Overflow