0

I have been trying to create a Ruby gem that simply exits my terminal whenever "x" is entered. Here is my main project file:

module SwissKnife
  VERSION = '0.0.1'

  class ConsoleUtility
    def exit
      `exit`
    end
  end
end

and my executable:

#!/usr/bin/env ruby

require 'swissknife'

util = SwissKnife::ConsoleUtility.new
util.exit

For some reason whenever I run this nothing appears to happen. I debugged it by adding in a simple puts 'Hello World!' in there, and it would print "Hello World!" but not exit. What am I doing wrong? Any help is greatly appreciated!

1 Answer 1

1
  1. Backticks execute code in a new shell
  2. exit isn't an executable that your shell runs, it's a special command understood by your shell - telling it to exit

So when you do

`exit`

it starts a shell which immediately exits. Not very useful. To exit the shell, you can instead kill Ruby's parent process.

Process.kill 'HUP', Process.ppid
Sign up to request clarification or add additional context in comments.

1 Comment

I don't think there is a way to send commands to the shell running Ruby, but you can kill the shell process. See my edit.

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.