Skip to content

Instantly share code, notes, and snippets.

@geelen
Created September 15, 2010 07:04
Show Gist options
  • Save geelen/580342 to your computer and use it in GitHub Desktop.
Save geelen/580342 to your computer and use it in GitHub Desktop.
The meat of shelling out safely was figured out by @notahat.
The idea for attaching to Symbol was from http://hans.fugal.net/blog/2007/11/03/backticks-2-0/
It makes me feel ill. With awesome.
class Symbol
def [](*args)
# a safe way to shell out
read_end, write_end = IO.pipe
pid = fork do
read_end.close
STDOUT.reopen(write_end)
STDERR.reopen(write_end)
exec(self.to_s, *args)
end
write_end.close
output = read_end.read
Process.waitpid(pid)
read_end.close
output
end
end
irb(main):046:0> puts :whoami[]
glen
=> nil
irb(main):047:0> puts :ls[]
Capfile
Gemfile
Gemfile.lock
README
Rakefile
app
compass
config
db
docs
features
lib
log
public
script
spec
test
tmp
vendor
=> nil
irb(main):048:0> puts :ls['-al']
total 96
drwxr-xr-x 27 glen staff 918 14 Sep 16:19 .
drwxr-xr-x 17 glen staff 578 31 Aug 10:25 ..
-rw-r--r--@ 1 glen staff 12292 19 Aug 18:36 .DS_Store
-rw-r--r-- 1 glen staff 564 4 Jan 2010 .autotest
drwxr-xr-x 3 glen staff 102 27 Jul 11:30 .bundle
drwxr-xr-x 22 glen staff 748 15 Sep 17:03 .git
-rw-r--r-- 1 glen staff 527 14 Sep 14:22 .gitignore
drwxr-xr-x 12 glen staff 408 15 Sep 17:03 .idea
-rw-r--r-- 1 glen staff 273 2 Sep 15:26 Capfile
-rw-r--r-- 1 glen staff 1607 14 Sep 16:19 Gemfile
-rw-r--r-- 1 glen staff 5504 14 Sep 16:19 Gemfile.lock
-rw-r--r-- 1 glen staff 515 20 Aug 11:21 README
-rw-r--r-- 1 glen staff 341 20 Aug 11:21 Rakefile
drwxr-xr-x 9 glen staff 306 2 Sep 15:26 app
drwxr-xr-x 4 glen staff 136 30 Jun 11:22 compass
drwxr-xr-x 42 glen staff 1428 14 Sep 16:19 config
drwxr-xr-x 8 glen staff 272 15 Sep 15:45 db
drwxr-xr-x 4 glen staff 136 20 Aug 11:21 docs
drwxr-xr-x 45 glen staff 1530 15 Sep 16:00 features
drwxr-xr-x 42 glen staff 1428 9 Sep 11:40 lib
drwxr-xr-x 16 glen staff 544 15 Sep 15:43 log
drwxr-xr-x 31 glen staff 1054 20 Aug 11:21 public
drwxr-xr-x 34 glen staff 1156 20 Aug 11:21 script
drwxr-xr-x 18 glen staff 612 15 Sep 15:45 spec
drwxr-xr-x 3 glen staff 102 11 Aug 10:53 test
drwxr-xr-x 69 glen staff 2346 15 Sep 15:39 tmp
drwxr-xr-x 6 glen staff 204 20 Aug 11:21 vendor
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment