cheatsheets/pry.md

154 lines
2.0 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
2012-11-25 04:13:51 +00:00
title: Pry
2015-11-24 05:02:17 +00:00
category: Ruby libraries
2013-10-14 02:36:58 +00:00
---
2012-11-25 04:13:51 +00:00
### About
{: .-intro}
Pry is a runtime development console for Ruby.
- <https://github.com/pry/pry>
2015-05-19 16:33:23 +00:00
### cd
2012-11-25 04:13:51 +00:00
2015-10-15 11:01:09 +00:00
```
> cd Array
```
2012-11-25 04:13:51 +00:00
2015-10-15 11:10:19 +00:00
```nohighlight
2015-10-15 11:01:09 +00:00
> ls
Array.methods: [] try_convert
Array#methods: & * + abbrev assoc at ...
```
2012-11-25 04:13:51 +00:00
2015-10-15 11:01:09 +00:00
```
> show-source
```
2012-11-25 04:13:51 +00:00
2015-05-19 16:33:23 +00:00
### Code
2012-11-25 04:13:51 +00:00
2015-10-15 11:10:19 +00:00
```nohighlight
2015-10-15 11:01:09 +00:00
> show-method Array#select
```
2012-11-25 04:13:51 +00:00
2015-05-19 16:33:23 +00:00
### Docs
2015-10-15 11:10:19 +00:00
```nohighlight
2015-10-15 11:01:09 +00:00
> ri Array
> ri Array#each
2015-05-19 16:33:23 +00:00
2015-10-15 11:01:09 +00:00
> cd Gem
> show-doc try_activate
```
2012-11-25 04:13:51 +00:00
2015-10-15 11:01:30 +00:00
### Finding
2012-11-25 04:13:51 +00:00
2015-10-15 11:10:19 +00:00
```nohighlight
2015-10-15 11:01:09 +00:00
> find-method each
Array#each
Array#each_index
Enumerable#each_slice
...
```
2012-11-25 04:13:51 +00:00
### Editing
2015-10-15 11:01:09 +00:00
> edit Pry#repl
2012-11-25 04:13:51 +00:00
### Gems
2013-10-14 02:36:58 +00:00
> gem-cd foo # Switch to gem's dir
> gem-install foo
> gem-list
2012-11-25 04:13:51 +00:00
### Misc commands
2013-10-14 02:36:58 +00:00
> hist # History
> wtf? # Trace of recent exception
2012-11-25 04:13:51 +00:00
2015-10-15 11:02:19 +00:00
## Rails
2012-11-25 04:13:51 +00:00
### Rails console
2015-10-15 11:01:09 +00:00
Also consider [pry-rails](https://rubygems.org/gems/pry-rails).
$ pry -r ./config/environment
2012-11-25 04:13:51 +00:00
2015-05-19 16:33:23 +00:00
### Rails
> show-models
> show-routes
> show-middleware
2015-10-15 11:01:09 +00:00
### ls
> ls # All
> ls -m # Methods
> ls -M # Instance methods
> ls -g # Globals
> ls -l # Local vars
> ls -c # Constants
> ls -i # Instance vars
2023-12-13 21:45:49 +00:00
> ls -G xx # Grep by regex
2015-10-15 11:01:09 +00:00
2015-10-15 11:02:19 +00:00
## Shell integration
2015-10-15 11:01:09 +00:00
2015-10-15 11:01:30 +00:00
shell-mode adds dir to the prompt.
2015-10-15 11:01:09 +00:00
pry(main)> shell-mode
pry(main):/home/x $
Commands with `.` are shell commands.
2015-10-15 11:02:46 +00:00
pry(main)> .cat hello.txt
2015-10-15 11:01:09 +00:00
2015-10-15 11:02:19 +00:00
## hirb
Add the [hirb](https://rubygems.org/gems/hirb) gem.
> table User.all
> view User.all
> view User.all, fields: %w[id name email]
2015-11-24 04:29:23 +00:00
## pry-rescue
Add the [pry-rescue](https://github.com/ConradIrwin/pry-rescue) gem.
```rb
Pry::rescue {
# raise exceptions here
}
```
Or run:
```
bundle exec rescue rspec
```
Additional commands:
```
pry(main)> cd-cause
pry(main)> try-again
```
## pry-remote
Add the [pry-remote](https://github.com/Mon-Ouie/pry-remote) gem.
```rb
# In your code:
binding.remote_pry
# In the shell:
bundle exec pry-remote
```
2015-10-15 11:02:19 +00:00
## Reference
2012-11-25 04:13:51 +00:00
* [Pry](https://github.com/pry/pry)
* [Hirb](https://github.com/cldwalker/hirb)