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

pry の中でも利用されている method_source gem を試す #ruby

Posted at

:gem: 概要

pry の中でも利用されている method_source gem を試します

インストール

pry を利用している方はインストール不要

$ gem install method_source

サンプル

サンプルコード: Methods#source_location

source_location.rb
require "set"
require "method_source"

class Hoge
  # hoge method header
  def hoge
    # before hoge
    "hoge"
    # after hoge
  end
end

class Child
  include MethodSource::ReeSourceLocation
end

# 自ファイルの source_location
print Hoge.instance_method(:hoge).source_location, "\n"

# source_location gem の source_location
print Child.new.method(:source_location).source_location
  • 出力確認
# 自ファイルの source_location
["source_location.rb", 6]
# source_location gem の source_location
["/path/to/ruby/gems/2.1.0/gems/method_source-0.8.2/lib/method_source/source_location.rb", 5]%

サンプルコード: Methods#source, Methods#source.comment

require "set"
require "method_source"

class Hoge
  # hoge method header
  def hoge
    # before hoge
    "hoge"
    # after hoge
  end
end

puts Hoge.instance_method(:hoge).source
puts Hoge.instance_method(:hoge).comment
  • 出力
  def hoge
    # before hoge
    "hoge"
    # after hoge
  end
# hoge method header

:books: 外部資料

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