LoginSignup
51
51

More than 5 years have passed since last update.

Ruby の 定番対話ツール pry 徹底攻略 | Documentation browsing #pry #ruby

Posted at

Ruby の 定番対話ツール pry 徹底攻略 | :book: Documentation browsing

概要

Ruby の 定番対話ツール pry 徹底攻略
ドキュメントの表示について

:book: Documentation browsing

新しいライブラリやコードベースにおいて、メソッドのドキュメントを参照することはとても重要です。
Pry は外部ライブラリに依存することなく、ドキュメントを参照可能です。
Pry は必要なドキュメントを実行時にファイルから取得します。
Pry は gem install 時に 事前生成されたドキュメントに依存しません。

:book: View method and class documentation

show-doc

show-doc でクラスやメソッドのドキュメントを参照します。
show-doc は RDoc と YARD の双方を理解し、シンタックスハイライトして表示します。

  • クラスのドキュメントを表示
[1] pry(main)> require 'prime'
=> true
[2] pry(main)> show-doc Prime

From: /home/path/to/ruby/2.0.0/prime.rb @ line 47:
Class name: Prime
Number of lines: 44

The set of all prime numbers.

== Example

  Prime.each(100) do |prime|
    p prime  #=> 2, 3, 5, 7, 11, ...., 97
  end

Prime is Enumerable:

  Prime.first 5 # => [2, 3, 5, 7, 11]

== Retrieving the instance

Prime.new is obsolete. Now Prime has the default instance and you can
access it as Prime.instance.

For convenience, each instance method of Prime.instance can be accessed
as a class method of Prime.

e.g.
  Prime.instance.prime?(2)  #=> true
  Prime.prime?(2)           #=> true

== Generators

A "generator" provides an implementation of enumerating pseudo-prime
numbers and it remembers the position of enumeration and upper bound.
Furthermore, it is a external iterator of prime enumeration which is
compatible to an Enumerator.

Prime::PseudoPrimeGenerator is the base class for generators.
There are few implementations of generator.

[Prime::EratosthenesGenerator]
  Uses eratosthenes's sieve.
[Prime::TrialDivisionGenerator]
  Uses the trial division method.
[Prime::Generator23]
  Generates all positive integers which is not divided by 2 nor 3.
  This sequence is very bad as a pseudo-prime sequence. But this
  is faster and uses much less memory than other generators. So,
  it is suitable for factorizing an integer which is not large but
  has many prime factors. e.g. for Prime#prime? .

※シンタックスハイライトの確認

pry_documentation_browsing1.png

  • クラスメソッドのドキュメントを表示
[2] pry(main)> show-doc Prime.instance

From: /home/path/to/ruby/2.0.0/prime.rb @ line 105:
Owner: #<Class:Prime>
Visibility: public
Signature: instance()
Number of lines: 1

Returns the default instance of Prime.
  • インスタンスメソッドのドキュメントを表示
[22] pry(main)> show-doc Prime#each

From: /home/path/to/ruby/2.0.0/prime.rb @ line 113:
Owner: Prime
Visibility: public
Signature: each(ubound=?, generator=?, &block)
Number of lines: 34

Iterates the given block over all prime numbers.

== Parameters

ubound::
  Optional. An arbitrary positive number.
  The upper bound of enumeration. The method enumerates
  prime numbers infinitely if mubound is nil.
generator::
  Optional. An implementation of pseudo-prime generator.

== Return value

An evaluated value of the given block at the last time.
Or an enumerator which is compatible to an Enumerator
if no block given.

== Description

Calls block once for each prime number, passing the prime as
a parameter.

ubound::
  Upper bound of prime numbers. The iterator stops after
  yields all prime numbers p <= mubound.

== Note

Prime.new returns a object extended by Prime::OldCompatibility
in order to compatibility to Ruby 1.8, and Prime#each is overwritten
by Prime::OldCompatibility#each.

Prime.new is now obsolete. Use Prime.instance.each or simply
Prime.each.
[23] pry(main)> 
  • show-doc のその他の機能 help で確認し、各自試してみてください
[25] pry(main)> show-doc --help
Usage:   show-doc [OPTIONS] [METH]
Aliases: ?

Show the documentation for a method or class. Tries instance methods first and
then methods by default.

show-doc hi_method # docs for hi_method
show-doc Pry       # for Pry class
show-doc Pry -a    # for all definitions of Pry class (all monkey patches)

    -s, --super             Select the 'super' method. Can be repeated to traverse the ancestors
    -l, --line-numbers      Show line numbers
    -b, --base-one          Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)
    -a, --all               Show all definitions and monkeypatches of the module/class
    -h, --help              Show this message.

:book: View basic method information

stat

stat でメソッドの基本情報を表示します。

[1] pry(main)> stat Enumerable#inject
Method Information:
--
Name: inject
Alias: reduce
Owner: Enumerable
Visibility: public
Type: Unbound
Arity: -1
Method Signature: inject(*arg1)
Source Location: Not found.

:book: Access ri documentation

ri

ri で通常の ri コマンド相当の機能を pry のセッションから呼び出せます。

[19] pry(Pry):1> ri String#upcase
String#upcase

(from ruby core)
------------------------------------------------------------------------------
  str.upcase   -> new_str

------------------------------------------------------------------------------

Returns a copy of str with all lowercase letters replaced with their
uppercase counterparts. The operation is locale insensitive---only characters
``a'' to ``z'' are affected. Note: case replacement is effective only in ASCII
region.

  "hEllO".upcase   #=> "HELLO"

:book::gem::heartbeat: View documentation for Ruby Core (C code)

pry の Plugin である pry-doc をインストールしていれば、
Ruby の core method の C のソースコードを表示できます。

  • prepare
$ gem install pry-doc
  • Kernel#puts のドキュメントを表示
[20] pry(Pry):1> show-doc Kernel#puts

From: io.c (C Method):
Owner: Kernel
Visibility: private
Signature: puts(*arg1)
Number of lines: 3

Equivalent to

    $stdout.puts(obj, ...)

:man::woman: 親記事

Ruby の 定番対話ツール pry 徹底攻略

:books: 外部資料

51
51
1

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
51
51