I was quite bored this morning and the coffee machine went into its 30
minute clean cycle, so I came up with this…
$ cat .irbrc
Dir["{,/usr,/usr/local}/bin/*"].each do |bin|
Object.instance_eval do
define_method(File.basename(bin)) do |*args|
args.map! { |arg| arg.to_s }
system bin, *args
end
end
end
$ irb --simple-prompt
irb: warn: can’t alias kill from irb_kill.
touch :foo
=> true
mv :foo, :bar
=> true
ls “-l”, :bar
-rw-r–r-- 1 andre andre 0 Oct 16 11:56 bar
=> true
Probably useless, I know… but running “vim :foo.rb” or “top” from
inside irb is cool
cool idea. i tweaked it to do lazy method definition:
harp:~ > cat .irbrc
module Kernel
PATH = ENV['PATH'].split File::PATH_SEPARATOR
end
class Object
def self.binattr bin
bin = bin.to_s
m = File.basename bin
unless respond_to?(m)
define_method(m){|*a| system bin, *a.map{|s| s.to_s}}
end
end
def which bin
b = bin.to_s
if b =~ %r|^#{ File::SEPARATOR }|
b
else
dir = PATH.detect{|d| test(?e, File.join(d,b))}
dir ? File.join(dir,b) : nil
end
end
alias_method "__mm__", "method_missing"
def method_missing m, *a, &b
bin = which m
if bin
Object.binattr(bin) and send(m,*a,&b)
else
__mm__(m,*a,&b)
end
end
end