Shell commands in irb

A

Andre Nathan

Hi

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.-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 :p

Andre
 
A

ara.t.howard

Hi

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

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


fun stuff!

-a
 
J

Joel VanderWerf

Andre said:
Heh, sweet. If only we didn't need the commas and the quotes around
command flags...

Yes, but it's better than having to use $var to evaluate a variable ;)
 
E

Eero Saynatkari

--y/XsGip80ttrIE8G
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi
=20
I was quite bored this morning and the coffee machine went into its 30
minute clean cycle, so I came up with this...
=20
$ cat .irbrc=20
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
=20
$ irb --simple-prompt
irb: warn: can't alias kill from irb_kill.-rw-r--r-- 1 andre andre 0 Oct 16 11:56 bar
=3D> true=20
=20
Probably useless, I know... but running "vim :foo.rb" or "top" from
inside irb is cool :p

You might find rs (the Ruby shell project) of interest! One of the
core tenets is to provide full access to Ruby at the command-line.

http://projects.kittensoft.org/rs/rdoc/indox.html

--y/XsGip80ttrIE8G
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFNEOQ7Nh7RM4TrhIRAthcAKDUKx38VEgN0hTAJ5MSytnvNXoi3ACdGEvO
u+sv/OLO41zmdl54s+Z3Yzk=
=bNbb
-----END PGP SIGNATURE-----

--y/XsGip80ttrIE8G--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ANN main-4.4.0 0
Old Paranoia Game in Python 15

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top