Pry – Part 2

More on using Pry.

Let’s play around with it a little using the Hello World gem (if you haven’t already installed it, use: gem install hello_world). Then, in pry:

require 'hello_world'

Some useful commands:

1. ls:
ls shows you which methods, constants and variables are accessible to Pry. By
default it shows you the local variables defined in the current shell, and any
public methods or instance variables defined on the current object.

So we can list, class methods:

ls HelloWorld -M
[]

i.e. nothing.

Instance methods:

ls HelloWorld -m
HelloWorld.methods: say_hello

i.e. this method is available.

And this is the default, i.e.

ls HelloWorld
HelloWorld.methods: say_hello

e.g.:

HelloWorld.say_hello
hello world
=> nil

2. cd
cd moves you into a new context (object or scope). As in UNIX shells use cd .. to go
back, cd / to return to Pry top-level and cd – to toggle between last two
scopes.

So,

cd HelloWorld

and now:

pry(HelloWorld):1> self
=> HelloWorld
pry(HelloWorld):1> ls -m
HelloWorld.methods: say_hello

So, we can invoke it like this:

pry(HelloWorld):1> say_hello
hello world
=> nil

However, if we’re only interested in the output value and not the return value then we can use a semi-colon:

pry(HelloWorld):1> say_hello;
hello world
pry(HelloWorld):1> 
[23] pry(HelloWorld):1> gem-cd hello_world
/Users/snowcrash/.rvm/gems/ruby-1.9.3-p327/gems/hello_world-0.0.2
[24] pry(HelloWorld):1> .pwd
/Users/snowcrash/.rvm/gems/ruby-1.9.3-p327/gems/hello_world-0.0.2

More in Pry – Part 3.

Leave a Reply

Your email address will not be published. Required fields are marked *