🖥

Ruby: Call method with absolute path.

2019/04/16に公開

Method call.

 Book.example 

If it is an absolute path, it can be written as follows.

 ::Book.example 

This is useful, for example, when calling a class of the same name from within a module.

 class Book def self.example p 'Class' end end module Example class Book #このモジュールの example を呼ぶ def self.call_module_method Book.example end # 普通のClass の example を呼ぶ def self.call_class_method ::Book.example end def self.example p 'Module' end end end Example::Book.call_module_method Example::Book.call_class_method 

Execution result

 "Module" "Class" 

  • I don't know if the word "absolute path" is correct or not, but I had a hard time knowing the keyword when I was making a note.
  • I thought I could call it in Class::Book.example but it was different. (The part of Class is ignored)

Original by

Ruby: 絶対パスでメソッドを呼び出す。

About

About this translattion

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-16

Discussion