문제

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?

도움이 되었습니까?

해결책

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

다른 팁

I assume it's some kind of pry's magic ;-)

After quick look at what's happening (I didn't look at pry's source), you might want to use this:

`ls`.split("\n")

or

Dir['./*']

What's good about this solution is that it will work outside of pry

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top