LoginSignup
1
0

More than 5 years have passed since last update.

pryでメソッドや、クラス、モジュールの定義を確認する

Posted at

pryにはshew-docというメソッドがある。

show-doc = メソッドや、クラス、モジュールの定義をpry上で直接確認できるコマンドです。
setクラスの実装を見てみましょう。

[1] pry(main)> require "set"
=> true
[2] pry(main)> show-source Set#add

From: /usr/lib/ruby/2.3.0/set.rb @ line 312:
Owner: Set
Visibility: public
Number of lines: 4

def add(o)
@hash[o] = true
self
end
[3] pry(main)> show-source Set

From: /usr/lib/ruby/2.3.0/set.rb @ line 69:
Class name: Set
Number of lines: 492

class Set
include Enumerable

# Creates a new set containing the given objects.
def self.
new(ary)
end

# Creates a new set containing the elements of the given enumerable
# object.
#
# If a block is given, the elements of enum are preprocessed by the
# given block.
def initialize(enum = nil, &block) # :yields: o
@hash ||= Hash.new(false)

enum.nil? and return

参考 パーフェクトRuby
良書です。

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