LoginSignup
9
7

More than 5 years have passed since last update.

Rubyのモジュール内でextend self

Posted at

defunkt/gistを読んでいたら,gist.rbに以下の記述を見つけた.

gist.rb
module Gist
  extend self
# ...
  def write(files, private_gist = false, description = nil)
# ...
end

extend selfでのレシーバーはGistなので,Gistモジュールの特異クラス(Gistクラス)に,selfで定義されているメソッド(つまりextend self以降で定義しているGistモジュールのメソッド.上記のwriteなど)を定義するということみたい.
結果Gist.writeのように呼び出すことが出来るようになる.

9
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7