Cheat Sheet速查表
跳转到主要内容

Pry 速查表

本文根据 Devhints / Pry 翻译。代码示例及版本范围沿用原文。

关于

Pry 是一个用于 Ruby 的运行时开发控制台。

cd

> cd Array
> ls
  Array.methods: [] try_convert
  Array#methods: & * + abbrev assoc at ...
> show-source

代码 (Code)

> show-method Array#select

文档

> ri Array
> ri Array#each

> cd Gem
> show-doc try_activate

查找

> find-method each
  Array#each
  Array#each_index
  Enumerable#each_slice
  ...

编辑

> edit Pry#repl

Gem(Ruby 库)

> gem-cd foo      # Switch to gem's dir
> gem-install foo
> gem-list

其他命令

> hist          # History
> wtf?          # Trace of recent exception

Rails

Rails 控制台

也可考虑 pry-rails.

$ pry -r ./config/environment

Rails

> show-models
> show-routes
> show-middleware

ls

> ls         # All

> ls -m      # Methods
> ls -M      # Instance methods

> ls -g      # Globals
> ls -l      # Local vars
> ls -c      # Constants

> ls -i      # Instance vars

> ls -G xx   # Grep by regex

Shell 集成

shell-mode 会将目录添加到提示符中。

pry(main)> shell-mode
pry(main):/home/x $

带有 . 的命令属于 shell 命令。

pry(main)> .cat hello.txt

hirb

添加 hirb gem。

> table User.all
> view User.all
> view User.all, fields: %w[id name email]

pry-rescue

添加 pry-rescue gem。

Pry::rescue {
  # raise exceptions here
}

或者运行:

bundle exec rescue rspec

其他命令:

pry(main)> cd-cause
pry(main)> try-again

pry-remote

添加 pry-remote gem。

# In your code:
binding.remote_pry

# In the shell:
bundle exec pry-remote

参考资料