0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ruby: Call method with absolute path.

Last updated at Posted at 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オープンチャットもご利用ください。

Twitter

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?