35
19

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 5 years have passed since last update.

メソッドの中からクラス名とメソッド名を取得する

Last updated at Posted at 2017-03-23

rubyのメソッド中において クラス名メソッド名 を取得する方法。

class Sample
  
  # @return [String]
  def self.class_name
    self.name   
  end

  # @return [Symbol]  
  def self.class_method_name
    __method__
  end

  # @return [String]
  def class_name
    self.class.name
  end

  # @return [Symbol]  
  def instance_method_name
    __method__
  end
  
end
> Sample.class_name
"Sample"

> Sample.class_method_name
:class_method_name

> sample = Sample.new
> sample.class_name
"Sample"

> sample.instance_method_name
:instance_method_name

Deprecateなメソッドが呼ばれた時など、ログを出力することに使うと良い感じ。

#{self.class.name}\##{__method__}"
35
19
2

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
35
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?