Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I capture the output of a pry shell command?

Tags:

shell

ruby

pry

I'm using pry and I want to capture, and work with output of a shell command.

For example, If I run

pry(main)> .ls

I want to get the list of files into an array that I can work with in Ruby.

How can I do this?

like image 372
Brian Avatar asked Feb 10 '12 17:02

Brian


1 Answers

This is a pretty old question but I'll answer it anyways. There are two primary methods of getting data out of pry commands. The first is if the command sets the keep_retval option to true, which the shell command does not. The second, is to use the virtual pipe. In your example this can be done as:

fizz = []
.ls | {|listing| fizz = listing.split("\n")} # can also be written as
.ls do |listing|
  fizz = listing.split("\n")
end
like image 79
bmatheny Avatar answered Oct 20 '22 09:10

bmatheny