Pry: Unable to inspect object built from class

Created on 13 Feb 2017  路  4Comments  路  Source: pry/pry

First off, thank you for making pry. It's an amazing tool that makes Ruby super fun to inspect 馃憦 馃憦 馃憦

This issue comes up when I instantiate a new instance of a Channel called potato. I can't cd into the object, but when I typo it shows that I could cd into an object called potato> as long as I do so with double quotes around it. Check it out:

    70: puts "    * If a channel is not used since #{end_time.strftime "%b %d %Y"}, archive it"
    71: puts "Starting review now...\n\n"
    72:
    73: channels_unarchived.each do |channel|
    74:     potato = Channel.new(channel)
 => 75:     binding.pry
    76:
    77:     # checking on last comment date time.
    78:     # grabbing the last 100 messages - max available for this API call.
    79:     msgs = client.channels_history(channel: channel.id).messages
    80:     human_msgs = msgs.reject(&:bot_id?)

[1] pry(main)> ls
ActiveSupport::ToJsonWithActiveSupportEncoder#methods: to_json
self.methods: inspect  to_s
locals:
  _      _ex_    _out_              channel              create_time          days_last_used      err         last_post_time  msg_warning  potato    unix
  __     _file_  _pry_              channels_unarchived  days_before_archive  days_since_created  five_years  last_week       msgs         response  warning_time
  _dir_  _in_    archived_channels  client               days_before_warn     end_time            human_msgs  msg_end_time    now          to_days   young_channel
[2] pry(main)> cd potato
Error: Bad object path: "potato"
Failed trying to resolve: "potato"
Exception: #<ArgumentError: wrong number of arguments (given 0, expected 1)>
[4] pry(main)> cd potat
Error: Bad object path: "potat"
Failed trying to resolve: "potat"
Exception: #<NameError: undefined local variable or method `potat' for main:Object
Did you mean?  potato>
[5] pry(main)> cd potato>
Error: Bad object path: "potato>"
Failed trying to resolve: "potato>"
Exception: #<SyntaxError: bot.rb:75: syntax error, unexpected end-of-input
potato>
       ^>
[6] pry(main)> cd "potato>"
[7] pry("potato>"):1>
[8] pry("potato>"):1>

For reference:

[3] pry(main)> show-source Channel

From: bot.rb @ line 37:
Class name: Channel
Number of lines: 9

class Channel < Hashie::Mash

  def initialize(args)
    args.each_pair do |k,v|
    self[k] = v
    end
  end

end

The most similar issue I could find was resolved by https://github.com/pry/pry/commit/bb5745aa5520f3531856b4ca4b29e508147636a1. Glad to help where I can in resolving it.

bug

Most helpful comment

Hey @midwire! I'm trying to reproduce it without Slack client or Hashie as dependencies and it's not having the same effect. I'll clean up the library I'm putting together and post it so we can move forward on this one. Thanks!

All 4 comments

@mbbroberg I'd like to try and help. Could you provide a minimal set of code to reproduce the problem. I've tried to reproduce the issue while making various assumptions but have not succeeded.

Hey @midwire! I'm trying to reproduce it without Slack client or Hashie as dependencies and it's not having the same effect. I'll clean up the library I'm putting together and post it so we can move forward on this one. Thanks!

This reproduces the issue for me:

require 'bundler/inline'

gemfile true do
  source 'https://rubygems.org'
  gem 'hashie'
  gem 'pry'
end

class Channel < Hashie::Mash
  def initialize(args)
    args.each_pair do |k,v|
    self[k] = v
    end
  end
end

potato = Channel.new({a: 1, b: 2})
binding.pry

try "cd potato":

1] pry(main)> cd potato
Error: Bad object path: "potato"
Failed trying to resolve: "potato"
Exception: #<ArgumentError: wrong number of arguments (given 0, expected 1)>

The root cause is that __binding__ fails for potato:

[1] pry(main)> potato.__binding__
ArgumentError: wrong number of arguments (given 0, expected 1)
from 1596.rb:12:in `initialize'

This is because potato reports back that it responds to __pry__ and when __binding__ invokes __pry__ on potato it raises.

Thus the test case can be reduced to the following:

require 'bundler/inline'

gemfile true do
  gem 'pry'
end

class X
  def respond_to? m
    true
  end
end

x = X.new
binding.pry

then cd x:

[1] pry(main)> cd x
Error: Bad object path: "x"
Failed trying to resolve: "x"
Exception: #<NameError: undefined local variable or method `__pry__' for #<X:0x0000557f37f36400>
Was this page helpful?
0 / 5 - 0 ratings