8
3

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.

Rubyでメソッドがあるかをrespond_to?で調べました。

Last updated at Posted at 2015-06-18

Rubyでオブジェクトにメソッドがあるか調べたくなります。

respond_to?を使う

調べたいオブジェクトでrespond_to?を使用します。

メソッドを判定する
hoge.respond_to?("message")

hogeオブジェクトがmessageを持っているとtrueが返りました。
Symbolか文字列で指定するメソッド名です。

おまけ:メソッド一覧は methods を使う

メソッド一覧を調べたいときはmethodsが便利と教えてもらいました。

メソッド一覧を表示
hoge.methods

参考

8
3
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
8
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?