ruby / ruby/irb

Implement `play` command

Open
#846 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Ruby
Stars
478
Forks
158
Avg merge
1d 12h
Merged PRs (30d)
12

Description

Description

Pry has a command called play that evaluates code blocks within the context of the REPL. This is an extremely useful method for iterative development in the REPL.

When using IRB as an iterative development environment or as a debugger this is extremely helpful to evaluate the effects of code blocks without either restarting the program flow or copy and pasting long chunks of text into the REPL.

play command help

The most useful part of play is the --lines flag.

play --lines 149..153 # assumes current context

For example a recent usecase for this.

Screenshot 2024-01-26 at 2 26 26 PM

I was attempting to debug this method. I knew that my "starting_bigrams" were not correct, but I didn't really know why. I could step through each of these lines of code in the debugger, but making changes to the loop on lines 20-22 required me to restart the method each time.

Using play - I was able to stop the interpreter on the binding.pry - execute a modified version of the loop on lines 20-22 and then repeatedly play -l 24..27 to see what the outcome would be. Because play executes in the context of the REPL it is able to modify local variables and enables rapid, iterative piece-wise development without littering the code with puts statements or stepping through the code multiple times.

        play -i 20 --lines 1..3 # assumes lines of the input expression at 20
        play -o 4               # the output of an expression at 4
        play Pry#repl -l 1..-1  # play the contents of Pry#repl method
        play -e 2               # play from specified line until end of valid expression
        play hello.rb           # play a file
        play Rakefile -l 5      # play line 5 of a file
        play -d hi              # play documentation of hi method
        play hi --open          # play hi method and leave it open

The play command in Pry has a whole lot of features, including playing snippets from other files, replaying literate documentation and connecting the inputs and outputs of expressions in code in REPL context. Most of these are predicated on the ability to load arbitrary code not via line input methods and replay it in the current context. Creating that capability I think would be sufficient for this use case - as well as giving us a feedback surface for further development.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading Pry's lib/pry/commands/play.rb and its command help, then compare the requested examples with IRB's existing command entry points. Define the smallest initial scope around loading arbitrary code in the current REPL context; done means code blocks can be evaluated there without line-by-line input, with behavior verified against the described play use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
cli, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.