Hacker News new | ask | show | jobs
by vidarh 4258 days ago
And with Ruby, especially since you mention Smalltalk, we have a tool that is getting closer to the kind of live introspection and modification that Smalltalk is famous for: Pry [1].

Pry lets you call "binding.pry" anywhere in your program to dump you into a shell within that context, with full access to local variables etc.. And tab-completion and plenty of introspection features. I frequently find myself triggering Pry in the middle of handling http requests if something doesn't work, for example. Letting me inspect the environment, modify stuff, and when I exit the request is completed.

It can also do things (with some limitations) like bring up an editor to where the current method was defined, and let you edit and reload the code.

And you can attach to it remotely using Drb in case the app in question doesn't run attached to a terminal.

At this point it's almost criminal to do Ruby development without Pry.

[1] http://pryrepl.org/

4 comments

I second Pry. It's amazing how fast you can use AR to add a little add-hoc business logic on-top of a DB and then drop a console with Pry to get a sort of custom interactive command line DSL :)

TBH if I was writing anything large enough for this kind of stuff to get out of hand, I would seriously consider a statically typed language like Golang or C#/F#(which both have absolutely fantastic tooling). REPL's don't help the next person to come along figure out what your methods are taking in and what those calls return. But I digress.

I don't always cut Ruby, but when I do I cut it with Pry... Yeah.

In pry is it possible to return something when I exit the REPL? Like this:

  def do_something
    return binding.pry

    do_something_but_does_not_work
  end

  result = do_something
  process result
It might be very convenient if I put 'return binging.pry' just before the broken code. I can interactively fix the broken code and continue run outside of current method.
If you are explicit: "return" in a Pry session will exit pry and return the value you pass to it, just as if you are in the method itself. So e.g. "return 42" would return 42 from do_something.
Pry changed my life for Rails development. Before it I was still using IRB and attempting to just log things to the terminal constantly to figure out what I could do. Pry has made me 2x as fast (if not more) as a Ruby developer.