Skip to content

Loading…

find-ref command #779

Closed
rking opened this Issue · 4 comments

4 participants

@rking

Let's say you have an object that you know is getting referenced, but you have no clue by what thing.

find-ref x

Bonus points if it doesn't also show the x being passed in.

Pseudocode:
search_space = local_vars + all_caller_bindings.map(&:local_vars) + all_ivars + all_constants + all_globals + ObjectSpace.each_object(Proc) { |p| p.binding }

@JackDanger

I recommend closing this issue because of it's age.

@JoshCheek

Played with the idea a little bit (https://gist.github.com/JoshCheek/d894a020b07586350d10) but it seems like this will be super fragile:

  • This finds closures (capture the local) and objects (capture the ivar) but we can see FindRef.call did not find itself, for that, I think we have to look at stack frames. And I don't know of any way to do that from Ruby-land
  • Any object that doesn't store its data in an ivar will need something explicitly written to handle this (e.g. Array, Hash, Struct, etc)
  • It's pretty heavy (iterates through ObjectSpace multiple times)
  • It's inherently prone to certain bugs (ie if it could traverse stack frame, it would find itself)
  • We could maybe use some of Ruby's methods to get it more reliable (http://ruby-doc.org/stdlib-2.1.0/libdoc/objspace/rdoc/ObjectSpace.html#method-c-reachable_objects_from) but even the docs say it's experimental and MRI only. Plus, some playing around with it found some pretty serious hurdles

I like the idea a lot (anything to demystify the Object web), but think it would be better to wait for someone to implement this finding functionality independently of Pry, then reconsider.

@ConradIrwin
pry member

I implemented with a patch to ruby (described http://cirw.in/blog/find-references). Using reachable_objects_from and the approach used there to discard references kept by the library itself is probably feasible, but should probably be implemented as a plugin not in core pry (as it would be MRI only)

@JoshCheek

Thaaaaat is fucking cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.