I'm executing a command with in ruby using open3, and Im setting a timeout using the safe_timeout gem (because of known issues with timeout reported here)
The code I have is very simple:
SafeTimeout.timeout(t) do
stdout, stdeerr, status = Open3.capture3(cmd)
@output = stdout
@result = status.exitstatus
@pid = status.pid
@timeout = t
end
The caveat here is that I only want to run this within the timeout block if t is defined.
Obviously I can use an if statement but then I'd have duplicate stuff and it doesn't feel very "ruby-like" to me.
Is there a nice way to do something like:
if t
timeout.do
command
end
else
command_without_timeout
end
?