Skip to content

Instantly share code, notes, and snippets.

@chsh
Created February 10, 2014 07:53
Show Gist options
  • Save chsh/8911963 to your computer and use it in GitHub Desktop.
Save chsh/8911963 to your computer and use it in GitHub Desktop.
You can chain string with shell command output. -> StdinString.new(`ls -l`).run('grep *.pid')
require 'tempfile'
class StdinString < String
def run(*commands)
r = `cat #{tf.path} | #{commands.join(' ')}`
remove_tf
StdinString.new r
end
private
def tf
@tf ||= build_tf
end
def remove_tf
if @tf
@tf.unlink
@tf = nil
end
end
def build_tf
f = Tempfile.new('stdin-string')
f.write(self)
f.close(false)
f
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment