2

I have a gem installed that displays a ascii image of a train moving across the terminal screen when someone types "ls". I also have a file called runner.rb.

If it's possible, how can I input the "ls" command to the terminal from within the ruby file?

1

3 Answers 3

3
cmd = "ls > somefile.txt"
system( cmd )

or even just simply

system( "ls" )

Thus, you can use system.

Sign up to request clarification or add additional context in comments.

Comments

2

You can also use ( ` ) character to execute Linux commands from ruby file.

e.g.

`ls` ## OR `whoami` etc.

Comments

2

If you want to execute the command as if you were sitting in the shell (so its output displays, etc.), use system.

> system('ls')
Look        At        All
My          Cool      Files
=> true

If you want to capture the output for use, use backticks.

> files = `ls`
=> "Look\nAt\nAll\nMy\nCool\nFiles\n"

You can interpolate in backticks if you want to (and obviously in the string you pass to system).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.