0
1

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の拡張されたメソッドの定義した場所を探す

Last updated at Posted at 2019-10-02

背景

先日書いた時期(Railsのblank?が優秀だった)の中で疑問が湧きました。blank? メソッドはどこに定義されているか、どうやって調べたらいいのか。

String#blanK? は、Rubyでは定義されておりませんでした。

irb(main):001:0> ''.blank?
Traceback (most recent call last):
        2: from /Users/yoshinori/.rbenv/versions/2.5.3/bin/irb:11:in `<main>'
        1: from (irb):1
NoMethodError (undefined method `blank?' for "":String)

じゃあ、なんで使えたんだろう。。
ということで、String#blank? についてはググるとRailsで用意されていることがわかりました。
しかし、もう少しマイナーなgemだったりしたらわからなかったかも...

良記事発見!!!

Rubyでメソッドの定義場所を見つける方法

irb(main):001:0> ''.method(:blank?).source_location
=> ["~/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/activesupport-5.2.3/lib/active_support/core_ext/object/blank.rb", 122]

どのgemのどのクラスのどの行で定義されているかが一発でわかりました。
最&高ですね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?