Why you should switch to Pry from IRB

I first heard about Pry as a replacement for IRB from this post by Jack Kinsella. I wasn’t impressed when I first read about it until I found a helpful feature that really interested me.



[1] pry(main)> arr = Array(1..5)
=> [1, 2, 3, 4, 5]


irb(main):001:0> arr = Array(1..5)
=> [1, 2, 3, 4, 5]


If you’re new to Ruby, unfamiliar with the language, or just curious as to what methods you can call what would you do next? Search the documentation? Google search? Or…


[2] pry(main)> cd arr

[3] pry(#):1> ls
all? collect_concat each_entry each_with_object find_all group_by max min minmax_by partition sort_by
any? detect each_slice entries flat_map inject max_by min_by none? reduce
chunk each_cons each_with_index find grep lazy member? minmax one? slice_before
& []= compact drop find_index insert pack reject rindex shuffle take uniq!
* assoc compact! drop_while first inspect permutation reject! rotate shuffle! take_while unshift
+ at concat each flatten join pop repeated_combination rotate! size to_a values_at
- bsearch count each_index flatten! keep_if pretty_print repeated_permutation sample slice to_ary zip
<< clear cycle empty? frozen? last pretty_print_cycle replace select slice! to_h |
<=> collect delete eql? hash length product reverse select! sort to_s
== collect! delete_at fetch include? map push reverse! shelljoin sort! transpose
[] combination delete_if fill index map! rassoc reverse_each shift sort_by! uniq
self.methods: __pry__
locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_


Yes! You can navigate an object as if you were navigating your file directory from the command line using familiar commands like ‘cd’ and ‘ls’.

With the pry-rails gem you can make pry the default console for your rails app instead of IRB.

Here I have a class ‘Widget’ and call the #all method.


[5] pry(Widget)Widget.all
Widget Load (0.4ms) SELECT "widgets".* FROM "widgets"

  Widget Load (0.4ms)  SELECT "widgets".* FROM "widgets"
=> [#<Widget id: 1, serial_number: "123456789", order_id: nil, created_at: "2014-08-19 20:41:52", updated_at: "2014-08-19 20:41:52">]



*assign a widget to a variable ‘w’
*navigate into it using the ‘cd’ command
*list the available methods using the ‘ls’ command (only some are listed here).


[6] pry(Widget):1> w = Widget.find(1)
Widget Load (0.3ms) SELECT "widgets".* FROM "widgets" WHERE "widgets"."id" = ? LIMIT 1 [["id", 1]]

=> #

[7] pry(Widget):1> cd w

[8] pry(#):2> ls
<=> connection_handler eql? frozen? hash inspect readonly? slice
== encode_with freeze has_transactional_callbacks? init_with readonly! set_transaction_state
becomes decrement delete destroyed? increment! persisted? toggle! update! update_attributes update_column
becomes! decrement! destroy! increment new_record? toggle update update_attribute update_attributes! update_columns
ActiveRecord::Scoping#methods: populate_with_current_scope_attributes
ActiveRecord::Sanitization#methods: quoted_id
ActiveRecord::AttributeAssignment#methods: assign_attributes attributes=
ActiveModel::Conversion#methods: to_model to_partial_path
ActiveRecord::Integration#methods: cache_key to_param
ActiveModel::Validations#methods: errors invalid? read_attribute_for_validation validates_with
ActiveSupport::Callbacks#methods: run_callbacks
validates_absence_of validates_confirmation_of validates_format_of validates_length_of validates_presence_of
validates_acceptance_of validates_exclusion_of validates_inclusion_of validates_numericality_of validates_size_of
ActiveRecord::Validations#methods: valid?
ActiveRecord::Locking::Optimistic#methods: locking_enabled?
ActiveRecord::Locking::Pessimistic#methods: lock! with_lock

Leave a Reply